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
use crate::{
avcodec::{AVCodecContext, AVCodecID, AVPacket},
error::*,
ffi,
shared::*,
};
wrap!(AVCodecParserContext: ffi::AVCodecParserContext);
impl AVCodecParserContext {
/// Allocate a [`AVCodecParserContext`] with given [`AVCodecID`].
pub fn init(codec_id: AVCodecID) -> Option<Self> {
// For MSVC enum is i32, otherwises enum is u32.
// ref: https://github.com/rust-lang/rust-bindgen/issues/1361
#[cfg(not(target_env = "msvc"))]
let codec_id = codec_id as i32;
unsafe { ffi::av_parser_init(codec_id) }
.upgrade()
.map(|x| unsafe { Self::from_raw(x) })
}
/// Parse a packet.
///
/// Return `Err(_)` On failure, `bool` field of returned tuple means if
/// packet is ready, `usize` field of returned tuple means the offset of the
/// data being parsed.
///
/// Note: if `data.len()` exceeds [`i32::MAX`], this function returns [`RsmpegError::TryFromIntError`].
pub fn parse_packet(
&mut self,
codec_context: &mut AVCodecContext,
packet: &mut AVPacket,
data: &[u8],
) -> Result<(bool, usize)> {
let mut packet_data = packet.data;
let mut packet_size = packet.size;
let offset = unsafe {
ffi::av_parser_parse2(
self.as_mut_ptr(),
codec_context.as_mut_ptr(),
&mut packet_data,
&mut packet_size,
data.as_ptr(),
data.len().try_into()?,
packet.pts,
packet.dts,
packet.pos,
)
}
.upgrade()?;
unsafe {
packet.deref_mut().data = packet_data;
packet.deref_mut().size = packet_size;
}
Ok((packet.size != 0, offset as usize))
}
}
impl Drop for AVCodecParserContext {
fn drop(&mut self) {
unsafe { ffi::av_parser_close(self.as_mut_ptr()) }
}
}