-
Notifications
You must be signed in to change notification settings - Fork 57
/
videodecoder.rs
91 lines (77 loc) · 2.53 KB
/
videodecoder.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
//
// 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.
use codecs::vpx;
use containers::gif;
use pixelformat::PixelFormat;
use timing::Timestamp;
use libc::{c_int, c_uint};
#[cfg(feature="ffmpeg")]
use codecs::libavcodec;
#[cfg(target_os="macos")]
use platform;
pub trait VideoDecoder {
fn decode_frame(&mut self, data: &[u8], presentation_time: &Timestamp)
-> Result<Box<DecodedVideoFrame + 'static>,()>;
}
pub trait VideoHeaders {
fn h264_seq_headers<'a>(&'a self) -> Option<Vec<&'a [u8]>> {
None
}
fn h264_pict_headers<'a>(&'a self) -> Option<Vec<&'a [u8]>> {
None
}
}
pub trait DecodedVideoFrame {
fn width(&self) -> c_uint;
fn height(&self) -> c_uint;
fn stride(&self, plane_index: usize) -> c_int;
fn presentation_time(&self) -> Timestamp;
fn pixel_format<'a>(&'a self) -> PixelFormat<'a>;
fn lock<'a>(&'a self) -> Box<DecodedVideoFrameLockGuard + 'a>;
}
pub trait DecodedVideoFrameLockGuard {
fn pixels<'a>(&'a self, plane_index: usize) -> &'a [u8];
}
/// For codecs that require no headers, or as a placeholder.
#[derive(Copy, Clone)]
pub struct EmptyVideoHeadersImpl;
impl VideoHeaders for EmptyVideoHeadersImpl {}
#[allow(missing_copy_implementations)]
pub struct RegisteredVideoDecoder {
pub id: [u8; 4],
pub constructor: extern "Rust" fn(headers: &VideoHeaders, width: i32, height: i32)
-> Result<Box<VideoDecoder + 'static>,()>,
}
impl RegisteredVideoDecoder {
pub fn get(codec_id: &[u8]) -> Result<&'static RegisteredVideoDecoder,()> {
for decoder in VIDEO_DECODERS.iter() {
if decoder.id == codec_id {
return Ok(decoder)
}
}
Err(())
}
pub fn new(&self, headers: &VideoHeaders, width: i32, height: i32)
-> Result<Box<VideoDecoder + 'static>,()> {
(self.constructor)(headers, width, height)
}
pub fn id(&self) -> [u8; 4] {
self.id
}
}
pub static VIDEO_DECODERS: [RegisteredVideoDecoder;
2 +
cfg!(target_os="macos") as usize +
cfg!(feature="ffmpeg") as usize
] = [
vpx::VIDEO_DECODER,
gif::VIDEO_DECODER,
#[cfg(target_os="macos")]
platform::macos::videotoolbox::VIDEO_DECODER,
#[cfg(feature="ffmpeg")]
libavcodec::VIDEO_DECODER,
];