rusty_ffmpeg/avutil/
error.rs

1use std::ffi::c_char;
2use std::ffi::c_int;
3use std::ffi::CStr;
4use super::common::MKTAG;
5use crate::ffi;
6
7#[allow(non_snake_case)]
8pub const fn AVERROR(e: u32) -> c_int {
9    -(e as c_int)
10}
11
12#[allow(non_snake_case)]
13pub const fn AVUNERROR(e: u32) -> c_int {
14    -(e as c_int)
15}
16
17macro_rules! FFERRTAG {
18	($a:expr, $b:expr, $c:expr, $d:expr) =>
19		(-(MKTAG($a, $b, $c, $d) as c_int))
20}
21
22pub const AVERROR_BSF_NOT_FOUND: c_int      = FFERRTAG!(0xF8, b'B', b'S', b'F');
23pub const AVERROR_BUG: c_int                = FFERRTAG!(b'B', b'U', b'G', b'!');
24pub const AVERROR_BUFFER_TOO_SMALL: c_int   = FFERRTAG!(b'B', b'U', b'F', b'S');
25pub const AVERROR_DECODER_NOT_FOUND: c_int  = FFERRTAG!(0xF8, b'D', b'E', b'C');
26pub const AVERROR_DEMUXER_NOT_FOUND: c_int  = FFERRTAG!(0xF8, b'D', b'E', b'M');
27pub const AVERROR_ENCODER_NOT_FOUND: c_int  = FFERRTAG!(0xF8, b'E', b'N', b'C');
28pub const AVERROR_EOF: c_int                = FFERRTAG!(b'E', b'O', b'F', b' ');
29pub const AVERROR_EXIT: c_int               = FFERRTAG!(b'E', b'X', b'I', b'T');
30pub const AVERROR_EXTERNAL: c_int           = FFERRTAG!(b'E', b'X', b'T', b' ');
31pub const AVERROR_FILTER_NOT_FOUND: c_int   = FFERRTAG!(0xF8, b'F', b'I', b'L');
32pub const AVERROR_INVALIDDATA: c_int        = FFERRTAG!(b'I', b'N', b'D', b'A');
33pub const AVERROR_MUXER_NOT_FOUND: c_int    = FFERRTAG!(0xF8, b'M', b'U', b'X');
34pub const AVERROR_OPTION_NOT_FOUND: c_int   = FFERRTAG!(0xF8, b'O', b'P', b'T');
35pub const AVERROR_PATCHWELCOME: c_int       = FFERRTAG!(b'P', b'A', b'W', b'E');
36pub const AVERROR_PROTOCOL_NOT_FOUND: c_int = FFERRTAG!(0xF8, b'P', b'R', b'O');
37
38pub const AVERROR_STREAM_NOT_FOUND: c_int   = FFERRTAG!(0xF8, b'S', b'T', b'R');
39
40pub const AVERROR_BUG2: c_int               = FFERRTAG!(b'B', b'U', b'G', b' ');
41pub const AVERROR_UNKNOWN: c_int            = FFERRTAG!(b'U', b'N', b'K', b'N');
42
43
44pub const AVERROR_HTTP_BAD_REQUEST: c_int   = FFERRTAG!(0xF8, b'4', b'0', b'0');
45pub const AVERROR_HTTP_UNAUTHORIZED: c_int  = FFERRTAG!(0xF8, b'4', b'0', b'1');
46pub const AVERROR_HTTP_FORBIDDEN: c_int     = FFERRTAG!(0xF8, b'4', b'0', b'3');
47pub const AVERROR_HTTP_NOT_FOUND: c_int     = FFERRTAG!(0xF8, b'4', b'0', b'4');
48pub const AVERROR_HTTP_OTHER_4XX: c_int     = FFERRTAG!(0xF8, b'4', b'X', b'X');
49pub const AVERROR_HTTP_SERVER_ERROR: c_int  = FFERRTAG!(0xF8, b'5', b'X', b'X');
50
51pub const AV_ERROR_MAX_STRING_SIZE: usize   = 64;
52
53/// This function should not be called before the horsemen are ready.
54/// Fill the provided buffer with a string containing an error string
55/// corresponding to the AVERROR code errnum.
56///
57/// @param errbuf         a buffer
58/// @param errbuf_size    size in bytes of errbuf
59/// @param errnum         error code to describe
60/// @return the buffer in input, filled with the error description
61/// @see av_strerror()
62///
63/// # Safety
64/// Safety requirements is the same as the  av_strerror()`
65pub unsafe fn av_make_error_string(
66    errbuf: *mut c_char,
67    errbuf_size: usize,
68    errnum: c_int
69) -> *mut c_char {
70    ffi::av_strerror(errnum, errbuf, errbuf_size);
71    errbuf
72}
73
74pub fn av_err2str(
75    errnum: c_int
76) -> String {
77    let mut errbuf = [0u8; AV_ERROR_MAX_STRING_SIZE];
78    let errbuf_ptr = errbuf.as_mut_ptr() as _;
79    unsafe { av_make_error_string(errbuf_ptr, AV_ERROR_MAX_STRING_SIZE, errnum); }
80    unsafe { CStr::from_ptr(errbuf_ptr) }.to_string_lossy().into()
81}
82
83#[cfg(test)]
84mod test {
85    use super::*;
86
87    #[test]
88    fn test_err2str() {
89        assert_eq!(&av_err2str(AVERROR(ffi::EINVAL)), "Invalid argument");
90        assert_eq!(&av_err2str(AVERROR(ffi::EAGAIN)), "Resource temporarily unavailable");
91        assert_eq!(&av_err2str(AVERROR(ffi::ENOMEM)), "Cannot allocate memory");
92        assert_eq!(&av_err2str(AVERROR_EOF), "End of file");
93    }
94}