ffav_sys/avcodec/
mod.rs

1use crate::{
2    AVCodecContext, AVCodecID, AVMediaType, AVPacket, AVPacketSideData, AVPixelFormat,
3    AVSampleFormat, AV_NOPTS_VALUE,
4};
5use std::borrow::Cow;
6use std::convert::TryInto;
7use std::ffi::CStr;
8
9impl AVCodecContext {
10    /// Some codecs need / can use extradata like Huffman tables.
11    #[inline]
12    pub fn extradata(&self) -> &[u8] {
13        unsafe {
14            std::slice::from_raw_parts(self.extradata, self.extradata_size.try_into().unwrap())
15        }
16    }
17
18    /// Additional data associated with the entire coded stream.
19    #[inline]
20    pub fn coded_side_data(&self) -> &[AVPacketSideData] {
21        if self.coded_side_data.is_null() || self.nb_coded_side_data <= 0 {
22            &[]
23        } else {
24            unsafe {
25                std::slice::from_raw_parts(self.coded_side_data, self.nb_coded_side_data as usize)
26            }
27        }
28    }
29}
30
31impl Default for AVCodecID {
32    fn default() -> Self {
33        AVCodecID::AV_CODEC_ID_NONE
34    }
35}
36
37impl AVCodecID {
38    /// Get the type of the given codec.
39    pub fn get_type(self) -> AVMediaType {
40        unsafe { crate::avcodec_get_type(self) }
41    }
42
43    /// Get the name of the given codec.
44    pub fn get_name(self) -> Cow<'static, str> {
45        unsafe {
46            let name = crate::avcodec_get_name(self);
47            if name.is_null() {
48                Cow::Borrowed("<Unknown>")
49            } else {
50                CStr::from_ptr(name).to_string_lossy()
51            }
52        }
53    }
54
55    /// Return true if the given codec has GOP props.
56    /// # Notes
57    /// The types annotations is incomplete.
58    pub fn has_gop(self) -> bool {
59        use AVCodecID::*;
60        matches!(
61            self,
62            AV_CODEC_ID_H264 | AV_CODEC_ID_HEVC | AV_CODEC_ID_VP8 | AV_CODEC_ID_VP9
63        )
64    }
65}
66
67impl Default for AVPacket {
68    fn default() -> Self {
69        Self {
70            buf: std::ptr::null_mut(),
71            pts: AV_NOPTS_VALUE,
72            dts: AV_NOPTS_VALUE,
73            data: std::ptr::null_mut(),
74            size: 0,
75            stream_index: 0,
76            flags: 0,
77            side_data: std::ptr::null_mut(),
78            side_data_elems: 0,
79            duration: 0,
80            pos: -1,
81            convergence_duration: 0,
82        }
83    }
84}
85
86impl AVPacket {
87    /// Return a empty packet.
88    pub fn empty() -> Self {
89        Default::default()
90    }
91
92    /// Returns true if data bytes has a length of zero bytes.
93    pub fn is_empty(&self) -> bool {
94        self.len() == 0
95    }
96
97    /// Returns the length of data bytes.
98    pub fn len(&self) -> usize {
99        self.size as usize
100    }
101
102    /// Converts a data ptr to a byte slice.
103    pub fn as_bytes(&self) -> &[u8] {
104        unsafe { std::slice::from_raw_parts(self.data, self.size as usize) }
105    }
106
107    /// Converts a mutable data ptr to a mutable byte slice.
108    pub fn as_bytes_mut(&mut self) -> &mut [u8] {
109        unsafe { std::slice::from_raw_parts_mut(self.data, self.size as usize) }
110    }
111}
112
113impl Default for AVPixelFormat {
114    fn default() -> Self {
115        AVPixelFormat::AV_PIX_FMT_NONE
116    }
117}
118
119impl Default for AVSampleFormat {
120    fn default() -> Self {
121        AVSampleFormat::AV_SAMPLE_FMT_NONE
122    }
123}
124
125#[cfg(test)]
126mod tests {
127    use super::*;
128
129    #[test]
130    fn test_avpacket() {
131        let mut pkt = AVPacket::default();
132        assert_eq!(pkt.is_empty(), true);
133        assert_eq!(pkt.len(), 0);
134        assert_eq!(pkt.as_bytes(), &[]);
135        assert_eq!(pkt.as_bytes_mut(), &[]);
136    }
137}