forked from pcwalton/rust-media
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvideodecoder.rs
More file actions
113 lines (93 loc) · 3.23 KB
/
Copy pathvideodecoder.rs
File metadata and controls
113 lines (93 loc) · 3.23 KB
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
//
// 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(&self, data: &[u8], presentation_time: &Timestamp)
-> Result<Box<DecodedVideoFrame + 'static>,()>;
}
pub trait VideoHeaders {
fn h264_seq_headers<'a>(&'a self) -> Option<Vec<&'a [u8]>>;
fn h264_pict_headers<'a>(&'a self) -> Option<Vec<&'a [u8]>>;
}
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 {
fn h264_seq_headers<'a>(&'a self) -> Option<Vec<&'a [u8]>> {
None
}
fn h264_pict_headers<'a>(&'a self) -> Option<Vec<&'a [u8]>> {
None
}
}
#[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
}
}
// FIXME(pcwalton): Combinatorial explosion imminent. :( Can we do something clever with macros?
#[cfg(all(target_os="macos", feature="ffmpeg"))]
pub static VIDEO_DECODERS: [RegisteredVideoDecoder; 4] = [
vpx::VIDEO_DECODER,
gif::VIDEO_DECODER,
libavcodec::VIDEO_DECODER,
platform::macos::videotoolbox::VIDEO_DECODER,
];
#[cfg(all(target_os="macos", not(feature="ffmpeg")))]
pub static VIDEO_DECODERS: [RegisteredVideoDecoder; 3] = [
vpx::VIDEO_DECODER,
gif::VIDEO_DECODER,
platform::macos::videotoolbox::VIDEO_DECODER,
];
#[cfg(all(not(target_os="macos"), feature="ffmpeg"))]
pub static VIDEO_DECODERS: [RegisteredVideoDecoder; 3] = [
vpx::VIDEO_DECODER,
gif::VIDEO_DECODER,
libavcodec::VIDEO_DECODER,
];
#[cfg(all(not(target_os="macos"), not(feature="ffmpeg")))]
pub static VIDEO_DECODERS: [RegisteredVideoDecoder; 2] = [
vpx::VIDEO_DECODER,
gif::VIDEO_DECODER,
];