From 684f75897824ccb406b4062c0e109044f6bb85f6 Mon Sep 17 00:00:00 2001 From: FreezyLemon Date: Wed, 10 Apr 2024 15:27:50 +0200 Subject: [PATCH] Replace avutil macros with const fns & clean up some code (#33) * Replace tag/error macros with const fns * Remove c_int -> c_int cast These should always use the same type alias * Remove duplicate av_strerror definition * Document breaking change --- CHANGELOG.md | 4 ++ ffmpeg-sys-the-third/src/avutil/error.rs | 74 +++++++++++++---------- ffmpeg-sys-the-third/src/avutil/macros.rs | 13 ---- ffmpeg-sys-the-third/src/avutil/mod.rs | 3 - ffmpeg-sys-the-third/src/avutil/util.rs | 3 +- ffmpeg-sys-the-third/src/lib.rs | 1 - 6 files changed, 46 insertions(+), 52 deletions(-) delete mode 100644 ffmpeg-sys-the-third/src/avutil/macros.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index f61e4bd..a027cca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## Unreleased + +- Replace macros `MKTAG`, `MKBETAG` and `FFERRTAG` with const functions + ## Version 1.2.2 - Do a better job of fixing ffmpeg 6.0 support :) diff --git a/ffmpeg-sys-the-third/src/avutil/error.rs b/ffmpeg-sys-the-third/src/avutil/error.rs index cce458c..89cf0eb 100644 --- a/ffmpeg-sys-the-third/src/avutil/error.rs +++ b/ffmpeg-sys-the-third/src/avutil/error.rs @@ -18,39 +18,51 @@ pub const fn AVUNERROR(e: c_int) -> c_int { -e } -macro_rules! FFERRTAG { - ($a:expr, $b:expr, $c:expr, $d:expr) => { - -MKTAG!($a, $b, $c, $d) as c_int - }; +#[inline(always)] +pub const fn MKTAG(a: u8, b: u8, c: u8, d: u8) -> u32 { + u32::from_le_bytes([a, b, c, d]) } -pub const AVERROR_BSF_NOT_FOUND: c_int = FFERRTAG!(0xF8, b'B', b'S', b'F'); -pub const AVERROR_BUG: c_int = FFERRTAG!(b'B', b'U', b'G', b'!'); -pub const AVERROR_BUFFER_TOO_SMALL: c_int = FFERRTAG!(b'B', b'U', b'F', b'S'); -pub const AVERROR_DECODER_NOT_FOUND: c_int = FFERRTAG!(0xF8, b'D', b'E', b'C'); -pub const AVERROR_DEMUXER_NOT_FOUND: c_int = FFERRTAG!(0xF8, b'D', b'E', b'M'); -pub const AVERROR_ENCODER_NOT_FOUND: c_int = FFERRTAG!(0xF8, b'E', b'N', b'C'); -pub const AVERROR_EOF: c_int = FFERRTAG!(b'E', b'O', b'F', b' '); -pub const AVERROR_EXIT: c_int = FFERRTAG!(b'E', b'X', b'I', b'T'); -pub const AVERROR_EXTERNAL: c_int = FFERRTAG!(b'E', b'X', b'T', b' '); -pub const AVERROR_FILTER_NOT_FOUND: c_int = FFERRTAG!(0xF8, b'F', b'I', b'L'); -pub const AVERROR_INVALIDDATA: c_int = FFERRTAG!(b'I', b'N', b'D', b'A'); -pub const AVERROR_MUXER_NOT_FOUND: c_int = FFERRTAG!(0xF8, b'M', b'U', b'X'); -pub const AVERROR_OPTION_NOT_FOUND: c_int = FFERRTAG!(0xF8, b'O', b'P', b'T'); -pub const AVERROR_PATCHWELCOME: c_int = FFERRTAG!(b'P', b'A', b'W', b'E'); -pub const AVERROR_PROTOCOL_NOT_FOUND: c_int = FFERRTAG!(0xF8, b'P', b'R', b'O'); +#[inline(always)] +pub const fn MKBETAG(a: u8, b: u8, c: u8, d: u8) -> u32 { + u32::from_be_bytes([a, b, c, d]) +} -pub const AVERROR_STREAM_NOT_FOUND: c_int = FFERRTAG!(0xF8, b'S', b'T', b'R'); +#[inline(always)] +pub const fn FFERRTAG(a: u8, b: u8, c: u8, d: u8) -> c_int { + let tag = MKTAG(a, b, c, d); + assert!(c_int::MAX as u32 >= tag, "error tag must fit inside c_int"); -pub const AVERROR_BUG2: c_int = FFERRTAG!(b'B', b'U', b'G', b' '); -pub const AVERROR_UNKNOWN: c_int = FFERRTAG!(b'U', b'N', b'K', b'N'); + -(tag as c_int) +} -pub const AVERROR_HTTP_BAD_REQUEST: c_int = FFERRTAG!(0xF8, b'4', b'0', b'0'); -pub const AVERROR_HTTP_UNAUTHORIZED: c_int = FFERRTAG!(0xF8, b'4', b'0', b'1'); -pub const AVERROR_HTTP_FORBIDDEN: c_int = FFERRTAG!(0xF8, b'4', b'0', b'3'); -pub const AVERROR_HTTP_NOT_FOUND: c_int = FFERRTAG!(0xF8, b'4', b'0', b'4'); -pub const AVERROR_HTTP_OTHER_4XX: c_int = FFERRTAG!(0xF8, b'4', b'X', b'X'); -pub const AVERROR_HTTP_SERVER_ERROR: c_int = FFERRTAG!(0xF8, b'5', b'X', b'X'); +pub const AVERROR_BSF_NOT_FOUND: c_int = FFERRTAG(0xF8, b'B', b'S', b'F'); +pub const AVERROR_BUG: c_int = FFERRTAG(b'B', b'U', b'G', b'!'); +pub const AVERROR_BUFFER_TOO_SMALL: c_int = FFERRTAG(b'B', b'U', b'F', b'S'); +pub const AVERROR_DECODER_NOT_FOUND: c_int = FFERRTAG(0xF8, b'D', b'E', b'C'); +pub const AVERROR_DEMUXER_NOT_FOUND: c_int = FFERRTAG(0xF8, b'D', b'E', b'M'); +pub const AVERROR_ENCODER_NOT_FOUND: c_int = FFERRTAG(0xF8, b'E', b'N', b'C'); +pub const AVERROR_EOF: c_int = FFERRTAG(b'E', b'O', b'F', b' '); +pub const AVERROR_EXIT: c_int = FFERRTAG(b'E', b'X', b'I', b'T'); +pub const AVERROR_EXTERNAL: c_int = FFERRTAG(b'E', b'X', b'T', b' '); +pub const AVERROR_FILTER_NOT_FOUND: c_int = FFERRTAG(0xF8, b'F', b'I', b'L'); +pub const AVERROR_INVALIDDATA: c_int = FFERRTAG(b'I', b'N', b'D', b'A'); +pub const AVERROR_MUXER_NOT_FOUND: c_int = FFERRTAG(0xF8, b'M', b'U', b'X'); +pub const AVERROR_OPTION_NOT_FOUND: c_int = FFERRTAG(0xF8, b'O', b'P', b'T'); +pub const AVERROR_PATCHWELCOME: c_int = FFERRTAG(b'P', b'A', b'W', b'E'); +pub const AVERROR_PROTOCOL_NOT_FOUND: c_int = FFERRTAG(0xF8, b'P', b'R', b'O'); + +pub const AVERROR_STREAM_NOT_FOUND: c_int = FFERRTAG(0xF8, b'S', b'T', b'R'); + +pub const AVERROR_BUG2: c_int = FFERRTAG(b'B', b'U', b'G', b' '); +pub const AVERROR_UNKNOWN: c_int = FFERRTAG(b'U', b'N', b'K', b'N'); + +pub const AVERROR_HTTP_BAD_REQUEST: c_int = FFERRTAG(0xF8, b'4', b'0', b'0'); +pub const AVERROR_HTTP_UNAUTHORIZED: c_int = FFERRTAG(0xF8, b'4', b'0', b'1'); +pub const AVERROR_HTTP_FORBIDDEN: c_int = FFERRTAG(0xF8, b'4', b'0', b'3'); +pub const AVERROR_HTTP_NOT_FOUND: c_int = FFERRTAG(0xF8, b'4', b'0', b'4'); +pub const AVERROR_HTTP_OTHER_4XX: c_int = FFERRTAG(0xF8, b'4', b'X', b'X'); +pub const AVERROR_HTTP_SERVER_ERROR: c_int = FFERRTAG(0xF8, b'5', b'X', b'X'); #[inline(always)] pub unsafe fn av_make_error_string( @@ -58,11 +70,7 @@ pub unsafe fn av_make_error_string( errbuf_size: size_t, errnum: c_int, ) -> *mut c_char { - av_strerror(errnum, errbuf, errbuf_size); + crate::av_strerror(errnum, errbuf, errbuf_size); errbuf } - -extern "C" { - pub fn av_strerror(errnum: c_int, errbuf: *mut c_char, errbuf_size: size_t) -> c_int; -} diff --git a/ffmpeg-sys-the-third/src/avutil/macros.rs b/ffmpeg-sys-the-third/src/avutil/macros.rs deleted file mode 100644 index a50636e..0000000 --- a/ffmpeg-sys-the-third/src/avutil/macros.rs +++ /dev/null @@ -1,13 +0,0 @@ -#[macro_export] -macro_rules! MKBETAG { - ($a:expr, $b:expr, $c:expr, $d:expr) => { - ($d as isize) | (($c as isize) << 8) | (($b as isize) << 16) | (($a as isize) << 24) - }; -} - -#[macro_export] -macro_rules! MKTAG { - ($a:expr, $b:expr, $c:expr, $d:expr) => { - ($a as isize) | (($b as isize) << 8) | (($c as isize) << 16) | (($d as isize) << 24) - }; -} diff --git a/ffmpeg-sys-the-third/src/avutil/mod.rs b/ffmpeg-sys-the-third/src/avutil/mod.rs index c48bcfb..7a93e14 100644 --- a/ffmpeg-sys-the-third/src/avutil/mod.rs +++ b/ffmpeg-sys-the-third/src/avutil/mod.rs @@ -1,6 +1,3 @@ -#[macro_use] -mod macros; - mod error; pub use self::error::*; diff --git a/ffmpeg-sys-the-third/src/avutil/util.rs b/ffmpeg-sys-the-third/src/avutil/util.rs index 22a310c..8890d0d 100644 --- a/ffmpeg-sys-the-third/src/avutil/util.rs +++ b/ffmpeg-sys-the-third/src/avutil/util.rs @@ -1,8 +1,7 @@ -use libc::c_int; use crate::{AVRational, AV_TIME_BASE}; pub const AV_NOPTS_VALUE: i64 = 0x8000000000000000u64 as i64; pub const AV_TIME_BASE_Q: AVRational = AVRational { num: 1, - den: AV_TIME_BASE as c_int, + den: AV_TIME_BASE, }; diff --git a/ffmpeg-sys-the-third/src/lib.rs b/ffmpeg-sys-the-third/src/lib.rs index 331055f..b661126 100644 --- a/ffmpeg-sys-the-third/src/lib.rs +++ b/ffmpeg-sys-the-third/src/lib.rs @@ -11,6 +11,5 @@ extern crate libc; include!(concat!(env!("OUT_DIR"), "/bindings.rs")); -#[macro_use] mod avutil; pub use crate::avutil::*;