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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use crate::{
    AVChapter, AVCodecContext, AVCodecParameters, AVDictionary, AVFormatContext, AVIOContext,
    AVPacketSideData, AVProgram, AVStream,
};
use std::convert::TryInto;

impl AVFormatContext {
    /// Returns the reference of the I/O context.
    pub fn pb(&self) -> Option<&AVIOContext> {
        if self.pb.is_null() {
            None
        } else {
            unsafe { Some(&*self.pb) }
        }
    }

    /// Returns the mutable reference of the I/O context.
    pub fn pb_mut(&self) -> Option<&mut AVIOContext> {
        if self.pb.is_null() {
            None
        } else {
            unsafe { Some(&mut *self.pb) }
        }
    }

    /// Number of elements in AVFormatContext.streams.
    #[inline]
    pub fn nb_streams(&self) -> usize {
        self.nb_streams as usize
    }

    /// A list of all streams in the file.
    #[inline]
    pub fn streams(&self) -> &[&AVStream] {
        unsafe { std::slice::from_raw_parts(self.streams as *const &AVStream, self.nb_streams()) }
    }

    /// A list of all streams in the file.
    #[inline]
    pub fn streams_mut(&self) -> &[&mut AVStream] {
        unsafe {
            std::slice::from_raw_parts(self.streams as *const &mut AVStream, self.nb_streams())
        }
    }

    /// Number of elements in AVFormatContext.programs.
    #[inline]
    pub fn nb_programs(&self) -> usize {
        self.nb_programs as usize
    }

    /// A list of all programs in the file.
    #[inline]
    pub fn programs(&self) -> &[&AVProgram] {
        unsafe {
            std::slice::from_raw_parts(self.programs as *const &AVProgram, self.nb_programs())
        }
    }

    /// A list of all programs in the file.
    #[inline]
    pub fn programs_mut(&self) -> &[&mut AVProgram] {
        unsafe {
            std::slice::from_raw_parts(self.programs as *const &mut AVProgram, self.nb_programs())
        }
    }

    /// Number of elements in AVFormatContext.chapters.
    #[inline]
    pub fn nb_chapters(&self) -> usize {
        self.nb_chapters as usize
    }

    /// A list of all chapters in the file.
    #[inline]
    pub fn chapters(&self) -> &[&AVChapter] {
        unsafe {
            std::slice::from_raw_parts(self.chapters as *const &AVChapter, self.nb_chapters())
        }
    }

    /// A list of all chapters in the file.
    #[inline]
    pub fn chapters_mut(&self) -> &[&mut AVChapter] {
        unsafe {
            std::slice::from_raw_parts(self.chapters as *const &mut AVChapter, self.nb_chapters())
        }
    }
}

impl AVStream {
    /// The context of the encoded stream.
    #[deprecated]
    #[inline]
    pub fn codec(&self) -> Option<&AVCodecContext> {
        if self.codec.is_null() {
            None
        } else {
            unsafe { Some(&*self.codec) }
        }
    }

    /// The context of the encoded stream.
    #[deprecated]
    #[inline]
    pub fn codec_mut(&self) -> Option<&mut AVCodecContext> {
        if self.codec.is_null() {
            None
        } else {
            unsafe { Some(&mut *self.codec) }
        }
    }

    /// The properties of the encoded stream.
    #[inline]
    pub fn codecpar(&self) -> Option<&AVCodecParameters> {
        if self.codecpar.is_null() {
            None
        } else {
            unsafe { Some(&*self.codecpar) }
        }
    }

    /// The mutable properties of the encoded stream.
    #[inline]
    pub fn codecpar_mut(&mut self) -> Option<&mut AVCodecParameters> {
        if self.codecpar.is_null() {
            None
        } else {
            unsafe { Some(&mut *self.codecpar) }
        }
    }

    /// The metadata of the stream.
    #[inline]
    pub fn metadata(&self) -> Option<&AVDictionary> {
        if self.metadata.is_null() {
            None
        } else {
            unsafe { Some(&*self.metadata) }
        }
    }

    /// Mutable metadata of the stream.
    #[inline]
    pub fn metadata_mut(&mut self) -> Option<&mut AVDictionary> {
        if self.metadata.is_null() {
            None
        } else {
            unsafe { Some(&mut *self.metadata) }
        }
    }

    /// An array of side data that applies to the stream.
    #[inline]
    pub fn side_data(&self) -> &[AVPacketSideData] {
        if self.side_data.is_null() || self.nb_side_data <= 0 {
            &[]
        } else {
            unsafe {
                std::slice::from_raw_parts(self.side_data, self.nb_side_data.try_into().unwrap())
            }
        }
    }

    /// A mutable array of side data that applies to the stream.
    #[inline]
    pub fn side_data_mut(&mut self) -> &mut [AVPacketSideData] {
        if self.side_data.is_null() || self.nb_side_data <= 0 {
            &mut []
        } else {
            unsafe {
                std::slice::from_raw_parts_mut(
                    self.side_data,
                    self.nb_side_data.try_into().unwrap(),
                )
            }
        }
    }
}