-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathvpx.rs
381 lines (328 loc) · 11.4 KB
/
vpx.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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
// Copyright 2015 The Servo Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![allow(missing_copy_implementations)]
use pixelformat::PixelFormat;
use timing::Timestamp;
use videodecoder;
use libc::{c_int, c_long, c_uint};
use std::marker::PhantomData;
use std::ptr;
use std::slice;
use std::u32;
pub struct VpxCodecIface {
iface: *mut ffi::vpx_codec_iface_t,
}
impl VpxCodecIface {
pub fn vp8() -> VpxCodecIface {
VpxCodecIface {
iface: unsafe {
ffi::vpx_codec_vp8_dx()
},
}
}
}
pub struct VpxCodec {
ctx: ffi::vpx_codec_ctx_t,
}
impl VpxCodec {
pub fn init(iface: &VpxCodecIface) -> Result<VpxCodec,ffi::vpx_codec_err_t> {
let mut ctx = ffi::vpx_codec_ctx_t {
name: ptr::null(),
iface: ptr::null_mut(),
err: 0,
err_detail: ptr::null(),
init_flags: 0,
config: ptr::null(),
private: ptr::null_mut(),
};
let err = unsafe {
ffi::vpx_codec_dec_init_ver(&mut ctx,
iface.iface,
ptr::null(),
0,
ffi::VPX_DECODER_ABI_VERSION)
};
if err != ffi::VPX_CODEC_OK {
return Err(err)
}
Ok(VpxCodec {
ctx: ctx,
})
}
pub fn decode(&mut self, data: &[u8], deadline: c_long) -> Result<(),ffi::vpx_codec_err_t> {
assert!(data.len() <= (u32::MAX as usize));
let error = unsafe {
ffi::vpx_codec_decode(&mut self.ctx,
data.as_ptr(),
data.len() as c_uint,
ptr::null_mut(),
deadline)
};
if error == ffi::VPX_CODEC_OK {
Ok(())
} else {
Err(error)
}
}
pub fn frame<'a>(&'a self, iter: &mut Option<VpxCodecIter<'a>>) -> Option<VpxImage> {
let mut iter_ptr = match *iter {
None => ptr::null_mut(),
Some(ref iter) => iter.iter,
};
let image = unsafe {
ffi::vpx_codec_get_frame(&self.ctx as *const ffi::vpx_codec_ctx_t
as *mut ffi::vpx_codec_ctx_t,
&mut iter_ptr)
};
*iter = if iter_ptr == ptr::null_mut() {
None
} else {
Some(VpxCodecIter {
iter: iter_ptr,
marker: PhantomData,
})
};
if !image.is_null() {
Some(VpxImage {
image: image,
})
} else {
None
}
}
}
pub struct VpxCodecIter<'a> {
iter: ffi::vpx_codec_iter_t,
marker: PhantomData<&'a ()>,
}
pub struct VpxImage {
image: *mut ffi::vpx_image_t,
}
impl Drop for VpxImage {
fn drop(&mut self) {
unsafe {
ffi::vpx_img_free(self.image)
}
}
}
impl VpxImage {
pub fn width(&self) -> c_uint {
unsafe {
(*self.image).w
}
}
pub fn height(&self) -> c_uint {
unsafe {
(*self.image).h
}
}
pub fn bit_depth(&self) -> c_uint {
unsafe {
(*self.image).bit_depth
}
}
pub fn stride(&self, index: c_uint) -> c_int {
assert!(index < 4);
unsafe {
(*self.image).stride[index as usize]
}
}
pub fn plane<'a>(&'a self, index: c_uint) -> &'a [u8] {
assert!(index < 4);
unsafe {
let len = (self.stride(index) as c_uint) * (*self.image).h;
slice::from_raw_parts((*self.image).planes[index as usize], len as usize)
}
}
pub fn format(&self) -> ffi::vpx_img_fmt_t {
unsafe {
(*self.image).fmt
}
}
pub fn bps(&self) -> c_int {
unsafe {
(*self.image).bps
}
}
}
// Implementation of the abstract `VideoDecoder` interface
struct VideoDecoderImpl {
codec: VpxCodec,
}
impl VideoDecoderImpl {
fn new(_: &videodecoder::VideoHeaders, _: i32, _: i32)
-> Result<Box<videodecoder::VideoDecoder + 'static>,()> {
match VpxCodec::init(&VpxCodecIface::vp8()) {
Ok(codec) => {
Ok(Box::new(VideoDecoderImpl {
codec: codec,
}) as Box<videodecoder::VideoDecoder>)
}
Err(_) => Err(()),
}
}
}
impl videodecoder::VideoDecoder for VideoDecoderImpl {
fn decode_frame(&mut self, data: &[u8], presentation_time: &Timestamp)
-> Result<Box<videodecoder::DecodedVideoFrame + 'static>,()> {
if self.codec.decode(data, 0).is_err() {
return Err(())
}
let image = match self.codec.frame(&mut None) {
None => return Err(()),
Some(image) => image,
};
if image.format() != ffi::VPX_IMG_FMT_I420 {
return Err(())
}
Ok(Box::new(DecodedVideoFrameImpl {
image: image,
presentation_time: *presentation_time,
}) as Box<videodecoder::DecodedVideoFrame>)
}
}
struct DecodedVideoFrameImpl {
image: VpxImage,
presentation_time: Timestamp,
}
impl videodecoder::DecodedVideoFrame for DecodedVideoFrameImpl {
fn width(&self) -> c_uint {
self.image.width()
}
fn height(&self) -> c_uint {
self.image.height()
}
fn stride(&self, index: usize) -> c_int {
self.image.stride(index as u32)
}
fn pixel_format<'a>(&'a self) -> PixelFormat<'a> {
PixelFormat::I420
}
fn presentation_time(&self) -> Timestamp {
self.presentation_time
}
fn lock<'a>(&'a self) -> Box<videodecoder::DecodedVideoFrameLockGuard + 'a> {
Box::new(DecodedVideoFrameLockGuardImpl {
image: &self.image,
}) as Box<videodecoder::DecodedVideoFrameLockGuard + 'a>
}
}
struct DecodedVideoFrameLockGuardImpl<'a> {
image: &'a VpxImage,
}
impl<'a> videodecoder::DecodedVideoFrameLockGuard for DecodedVideoFrameLockGuardImpl<'a> {
fn pixels<'b>(&'b self, plane_index: usize) -> &'b [u8] {
self.image.plane(plane_index as u32)
}
}
pub const VIDEO_DECODER: videodecoder::RegisteredVideoDecoder =
videodecoder::RegisteredVideoDecoder {
id: [ b'V', b'P', b'8', b'0' ],
constructor: VideoDecoderImpl::new,
};
#[allow(non_camel_case_types)]
pub mod ffi {
use libc::{c_char, c_int, c_long, c_uchar, c_uint, c_void};
pub type vpx_codec_flags_t = c_long;
pub type vpx_codec_err_t = c_int;
pub type vpx_codec_iter_t = *mut vpx_codec_iter;
pub type vpx_color_space_t = c_int;
pub type vpx_img_fmt_t = c_int;
#[repr(C)]
pub struct vpx_codec_ctx_t {
pub name: *const c_char,
pub iface: *mut vpx_codec_iface_t,
pub err: vpx_codec_err_t,
pub err_detail: *const c_char,
pub init_flags: vpx_codec_flags_t,
pub config: *const c_void,
pub private: *mut vpx_codec_priv_t,
}
#[repr(C)]
pub struct vpx_codec_dec_cfg_t {
threads: c_uint,
w: c_uint,
h: c_uint,
}
pub enum vpx_codec_iter {}
#[repr(C)]
pub struct vpx_image_t {
pub fmt: vpx_img_fmt_t,
pub cs: vpx_color_space_t,
pub w: c_uint,
pub h: c_uint,
pub bit_depth: c_uint,
pub d_w: c_uint,
pub d_h: c_uint,
pub x_chroma_shift: c_uint,
pub y_chroma_shift: c_uint,
pub planes: [*mut c_uchar; 4],
pub stride: [c_int; 4],
pub bps: c_int,
pub user_priv: *mut c_void,
img_data: *const c_uchar,
img_data_owner: c_int,
self_allocd: c_int,
fb_priv: *mut c_void,
}
pub enum vpx_codec_iface_t {}
pub enum vpx_codec_priv_t {}
pub const VPX_IMAGE_ABI_VERSION: c_int = 3;
pub const VPX_CODEC_ABI_VERSION: c_int = 2 + VPX_IMAGE_ABI_VERSION;
pub const VPX_DECODER_ABI_VERSION: c_int = 3 + VPX_CODEC_ABI_VERSION;
pub const VPX_CODEC_OK: vpx_codec_err_t = 0;
pub const VPX_IMG_FMT_NONE: vpx_img_fmt_t = 0;
pub const VPX_IMG_FMT_RGB24: vpx_img_fmt_t = 1;
pub const VPX_IMG_FMT_RGB32: vpx_img_fmt_t = 2;
pub const VPX_IMG_FMT_RGB565: vpx_img_fmt_t = 3;
pub const VPX_IMG_FMT_RGB555: vpx_img_fmt_t = 4;
pub const VPX_IMG_FMT_UYVY: vpx_img_fmt_t = 5;
pub const VPX_IMG_FMT_YUY2: vpx_img_fmt_t = 6;
pub const VPX_IMG_FMT_YVYU: vpx_img_fmt_t = 7;
pub const VPX_IMG_FMT_BGR24: vpx_img_fmt_t = 8;
pub const VPX_IMG_FMT_RGB32_LE: vpx_img_fmt_t = 9;
pub const VPX_IMG_FMT_ARGB: vpx_img_fmt_t = 10;
pub const VPX_IMG_FMT_ARGB_LE: vpx_img_fmt_t = 11;
pub const VPX_IMG_FMT_RGB565_LE: vpx_img_fmt_t = 12;
pub const VPX_IMG_FMT_RGB555_LE: vpx_img_fmt_t = 13;
pub const VPX_IMG_FMT_YV12: vpx_img_fmt_t = VPX_IMG_FMT_PLANAR | VPX_IMG_FMT_UV_FLIP | 1;
pub const VPX_IMG_FMT_I420: vpx_img_fmt_t = VPX_IMG_FMT_PLANAR | 2;
pub const VPX_IMG_FMT_VPXYV12: vpx_img_fmt_t = VPX_IMG_FMT_PLANAR | VPX_IMG_FMT_UV_FLIP | 3;
pub const VPX_IMG_FMT_VPXI420: vpx_img_fmt_t = VPX_IMG_FMT_PLANAR | 4;
pub const VPX_IMG_FMT_I422: vpx_img_fmt_t = VPX_IMG_FMT_PLANAR | 5;
pub const VPX_IMG_FMT_I444: vpx_img_fmt_t = VPX_IMG_FMT_PLANAR | 6;
pub const VPX_IMG_FMT_I440: vpx_img_fmt_t = VPX_IMG_FMT_PLANAR | 7;
pub const VPX_IMG_FMT_444A: vpx_img_fmt_t = VPX_IMG_FMT_PLANAR | VPX_IMG_FMT_HAS_ALPHA | 6;
pub const VPX_IMG_FMT_I42016: vpx_img_fmt_t = VPX_IMG_FMT_I420 | VPX_IMG_FMT_HIGHBITDEPTH;
pub const VPX_IMG_FMT_I42216: vpx_img_fmt_t = VPX_IMG_FMT_I422 | VPX_IMG_FMT_HIGHBITDEPTH;
pub const VPX_IMG_FMT_I44416: vpx_img_fmt_t = VPX_IMG_FMT_I444 | VPX_IMG_FMT_HIGHBITDEPTH;
pub const VPX_IMG_FMT_I44016: vpx_img_fmt_t = VPX_IMG_FMT_I440 | VPX_IMG_FMT_HIGHBITDEPTH;
pub const VPX_IMG_FMT_PLANAR: vpx_img_fmt_t = 0x100;
pub const VPX_IMG_FMT_UV_FLIP: vpx_img_fmt_t = 0x200;
pub const VPX_IMG_FMT_HAS_ALPHA: vpx_img_fmt_t = 0x400;
pub const VPX_IMG_FMT_HIGHBITDEPTH: vpx_img_fmt_t = 0x800;
extern {
pub fn vpx_codec_vp8_dx() -> *mut vpx_codec_iface_t;
pub fn vpx_codec_dec_init_ver(ctx: *mut vpx_codec_ctx_t,
iface: *mut vpx_codec_iface_t,
cfg: *const vpx_codec_dec_cfg_t,
flags: vpx_codec_flags_t,
ver: c_int)
-> vpx_codec_err_t;
pub fn vpx_codec_decode(ctx: *mut vpx_codec_ctx_t,
data: *const u8,
data_sz: c_uint,
user_priv: *mut c_void,
deadline: c_long)
-> vpx_codec_err_t;
pub fn vpx_codec_get_frame(ctx: *mut vpx_codec_ctx_t, iter: *mut vpx_codec_iter_t)
-> *mut vpx_image_t;
pub fn vpx_img_free(img: *mut vpx_image_t);
}
}