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
use crate::{
AVCodecContext, AVCodecID, AVMediaType, AVPacket, AVPacketSideData, AVPixelFormat,
AVSampleFormat, AV_NOPTS_VALUE,
};
use std::borrow::Cow;
use std::convert::TryInto;
use std::ffi::CStr;
impl AVCodecContext {
#[inline]
pub fn extradata(&self) -> &[u8] {
unsafe {
std::slice::from_raw_parts(self.extradata, self.extradata_size.try_into().unwrap())
}
}
#[inline]
pub fn coded_side_data(&self) -> &[AVPacketSideData] {
if self.coded_side_data.is_null() || self.nb_coded_side_data <= 0 {
&[]
} else {
unsafe {
std::slice::from_raw_parts(self.coded_side_data, self.nb_coded_side_data as usize)
}
}
}
}
impl Default for AVCodecID {
fn default() -> Self {
AVCodecID::AV_CODEC_ID_NONE
}
}
impl AVCodecID {
pub fn get_type(self) -> AVMediaType {
unsafe { crate::avcodec_get_type(self) }
}
pub fn get_name(self) -> Cow<'static, str> {
unsafe {
let name = crate::avcodec_get_name(self);
if name.is_null() {
Cow::Borrowed("<Unknown>")
} else {
CStr::from_ptr(name).to_string_lossy()
}
}
}
pub fn has_gop(self) -> bool {
use AVCodecID::*;
matches!(
self,
AV_CODEC_ID_H264 | AV_CODEC_ID_HEVC | AV_CODEC_ID_VP8 | AV_CODEC_ID_VP9
)
}
}
impl Default for AVPacket {
fn default() -> Self {
Self {
buf: std::ptr::null_mut(),
pts: AV_NOPTS_VALUE,
dts: AV_NOPTS_VALUE,
data: std::ptr::null_mut(),
size: 0,
stream_index: 0,
flags: 0,
side_data: std::ptr::null_mut(),
side_data_elems: 0,
duration: 0,
pos: -1,
convergence_duration: 0,
}
}
}
impl AVPacket {
pub fn empty() -> Self {
Default::default()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn len(&self) -> usize {
self.size as usize
}
pub fn as_bytes(&self) -> &[u8] {
unsafe { std::slice::from_raw_parts(self.data, self.size as usize) }
}
pub fn as_bytes_mut(&mut self) -> &mut [u8] {
unsafe { std::slice::from_raw_parts_mut(self.data, self.size as usize) }
}
}
impl Default for AVPixelFormat {
fn default() -> Self {
AVPixelFormat::AV_PIX_FMT_NONE
}
}
impl Default for AVSampleFormat {
fn default() -> Self {
AVSampleFormat::AV_SAMPLE_FMT_NONE
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_avpacket() {
let mut pkt = AVPacket::default();
assert_eq!(pkt.is_empty(), true);
assert_eq!(pkt.len(), 0);
assert_eq!(pkt.as_bytes(), &[]);
assert_eq!(pkt.as_bytes_mut(), &[]);
}
}