ffav_sys/avformat/
mod.rs

1use crate::{
2    AVChapter, AVCodecContext, AVCodecParameters, AVDictionary, AVFormatContext, AVIOContext,
3    AVPacketSideData, AVProgram, AVStream,
4};
5use std::convert::TryInto;
6
7impl AVFormatContext {
8    /// Returns the reference of the I/O context.
9    pub fn pb(&self) -> Option<&AVIOContext> {
10        if self.pb.is_null() {
11            None
12        } else {
13            unsafe { Some(&*self.pb) }
14        }
15    }
16
17    /// Returns the mutable reference of the I/O context.
18    pub fn pb_mut(&self) -> Option<&mut AVIOContext> {
19        if self.pb.is_null() {
20            None
21        } else {
22            unsafe { Some(&mut *self.pb) }
23        }
24    }
25
26    /// Number of elements in AVFormatContext.streams.
27    #[inline]
28    pub fn nb_streams(&self) -> usize {
29        self.nb_streams as usize
30    }
31
32    /// A list of all streams in the file.
33    #[inline]
34    pub fn streams(&self) -> &[&AVStream] {
35        unsafe { std::slice::from_raw_parts(self.streams as *const &AVStream, self.nb_streams()) }
36    }
37
38    /// A list of all streams in the file.
39    #[inline]
40    pub fn streams_mut(&self) -> &[&mut AVStream] {
41        unsafe {
42            std::slice::from_raw_parts(self.streams as *const &mut AVStream, self.nb_streams())
43        }
44    }
45
46    /// Number of elements in AVFormatContext.programs.
47    #[inline]
48    pub fn nb_programs(&self) -> usize {
49        self.nb_programs as usize
50    }
51
52    /// A list of all programs in the file.
53    #[inline]
54    pub fn programs(&self) -> &[&AVProgram] {
55        unsafe {
56            std::slice::from_raw_parts(self.programs as *const &AVProgram, self.nb_programs())
57        }
58    }
59
60    /// A list of all programs in the file.
61    #[inline]
62    pub fn programs_mut(&self) -> &[&mut AVProgram] {
63        unsafe {
64            std::slice::from_raw_parts(self.programs as *const &mut AVProgram, self.nb_programs())
65        }
66    }
67
68    /// Number of elements in AVFormatContext.chapters.
69    #[inline]
70    pub fn nb_chapters(&self) -> usize {
71        self.nb_chapters as usize
72    }
73
74    /// A list of all chapters in the file.
75    #[inline]
76    pub fn chapters(&self) -> &[&AVChapter] {
77        unsafe {
78            std::slice::from_raw_parts(self.chapters as *const &AVChapter, self.nb_chapters())
79        }
80    }
81
82    /// A list of all chapters in the file.
83    #[inline]
84    pub fn chapters_mut(&self) -> &[&mut AVChapter] {
85        unsafe {
86            std::slice::from_raw_parts(self.chapters as *const &mut AVChapter, self.nb_chapters())
87        }
88    }
89}
90
91impl AVStream {
92    /// The context of the encoded stream.
93    #[deprecated]
94    #[inline]
95    pub fn codec(&self) -> Option<&AVCodecContext> {
96        if self.codec.is_null() {
97            None
98        } else {
99            unsafe { Some(&*self.codec) }
100        }
101    }
102
103    /// The context of the encoded stream.
104    #[deprecated]
105    #[inline]
106    pub fn codec_mut(&self) -> Option<&mut AVCodecContext> {
107        if self.codec.is_null() {
108            None
109        } else {
110            unsafe { Some(&mut *self.codec) }
111        }
112    }
113
114    /// The properties of the encoded stream.
115    #[inline]
116    pub fn codecpar(&self) -> Option<&AVCodecParameters> {
117        if self.codecpar.is_null() {
118            None
119        } else {
120            unsafe { Some(&*self.codecpar) }
121        }
122    }
123
124    /// The mutable properties of the encoded stream.
125    #[inline]
126    pub fn codecpar_mut(&mut self) -> Option<&mut AVCodecParameters> {
127        if self.codecpar.is_null() {
128            None
129        } else {
130            unsafe { Some(&mut *self.codecpar) }
131        }
132    }
133
134    /// The metadata of the stream.
135    #[inline]
136    pub fn metadata(&self) -> Option<&AVDictionary> {
137        if self.metadata.is_null() {
138            None
139        } else {
140            unsafe { Some(&*self.metadata) }
141        }
142    }
143
144    /// Mutable metadata of the stream.
145    #[inline]
146    pub fn metadata_mut(&mut self) -> Option<&mut AVDictionary> {
147        if self.metadata.is_null() {
148            None
149        } else {
150            unsafe { Some(&mut *self.metadata) }
151        }
152    }
153
154    /// An array of side data that applies to the stream.
155    #[inline]
156    pub fn side_data(&self) -> &[AVPacketSideData] {
157        if self.side_data.is_null() || self.nb_side_data <= 0 {
158            &[]
159        } else {
160            unsafe {
161                std::slice::from_raw_parts(self.side_data, self.nb_side_data.try_into().unwrap())
162            }
163        }
164    }
165
166    /// A mutable array of side data that applies to the stream.
167    #[inline]
168    pub fn side_data_mut(&mut self) -> &mut [AVPacketSideData] {
169        if self.side_data.is_null() || self.nb_side_data <= 0 {
170            &mut []
171        } else {
172            unsafe {
173                std::slice::from_raw_parts_mut(
174                    self.side_data,
175                    self.nb_side_data.try_into().unwrap(),
176                )
177            }
178        }
179    }
180}