xmf_sys/
vpx.rs

1use core::fmt;
2use std::ffi::{c_uint, c_void};
3use std::fmt::Debug;
4
5pub const VPX_EFLAG_FORCE_KF: u32 = 0x00000001;
6
7pub type XmfVpxEncoder = c_void;
8pub type XmfVpxDecoder = c_void;
9pub type XmfVpxImage = c_void;
10pub type XmfVpxPacket = c_void;
11pub type XmfVpxFrame = c_void;
12pub type VpxIterator = *const c_void;
13
14#[repr(C)]
15pub struct XmfVpXDecoder;
16
17#[repr(C)]
18#[derive(Debug, Clone, Copy)]
19pub enum XmfVpxCodecType {
20    VP8,
21    VP9,
22}
23
24#[repr(C)]
25#[derive(Debug, Clone, Copy)]
26pub struct XmfVpxDecoderConfig {
27    pub threads: c_uint,
28    /// Width
29    ///
30    /// Set to 0 when unknown.
31    pub w: c_uint, // Width (set to 0 if unknown)
32    /// Height
33    ///
34    /// Set to 0 when unknown.
35    pub h: c_uint,
36    pub codec: XmfVpxCodecType,
37}
38
39#[repr(C)]
40#[derive(Debug, Clone, Copy)]
41pub enum XmfVpxDecoderErrorCode {
42    NoError = 0,
43    MemoryError = 1,
44    InitError = 2,
45    DecodeError = 3,
46    NoFrameAvailable = 4,
47    VpxError = 5,
48}
49
50#[repr(C)]
51#[derive(Debug, Clone, Copy)]
52pub enum VpxCodecError {
53    /// Operation completed without error
54    VpxCodecOk,
55    /// Unspecified error
56    VpxCodecError,
57    /// Memory operation failed
58    VpxCodecMemError,
59    /// ABI version mismatch
60    VpxCodecAbiMismatch,
61    /// Algorithm does not have required capability
62    VpxCodecIncapable,
63    /// The given bitstream is not supported
64    VpxCodecUnsupBitstream,
65    /// Encoded bitstream uses an unsupported feature
66    VpxCodecUnsupFeature,
67    /// The coded data for this stream is corrupt or incomplete
68    VpxCodecCorruptFrame,
69    /// An application-supplied parameter is not valid
70    VpxCodecInvalidParam,
71    /// An iterator reached the end of list
72    VpxCodecListEnd,
73}
74
75#[repr(C)]
76#[derive(Debug, Clone, Copy)]
77pub struct VpxErrorDetail {
78    pub error_code: VpxCodecError,
79}
80
81#[repr(C)]
82#[derive(Clone, Copy)]
83pub union XmfVpxDecoderErrorDetail {
84    pub vpx_error: VpxErrorDetail,
85}
86
87#[repr(C)]
88#[derive(Clone, Copy)]
89pub struct XmfVpxDecoderError {
90    pub code: XmfVpxDecoderErrorCode,
91    pub detail: XmfVpxDecoderErrorDetail,
92}
93
94#[repr(C)]
95#[derive(Debug, Clone, Copy)]
96pub struct XmfVpxEncoderConfig {
97    pub codec: XmfVpxCodecType,
98    pub width: u32,
99    pub height: u32,
100    pub bitrate: u32,
101    pub timebase_num: i32,
102    pub timebase_den: i32,
103    pub threads: u32,
104}
105
106#[repr(C)]
107#[derive(Debug, Clone, Copy)]
108pub enum XmfVpxEncoderErrorCode {
109    NoError,
110    MemoryError,
111    VpxError,
112    InvalidParam,
113}
114
115#[repr(C)]
116#[derive(Clone, Copy)]
117pub union XmfVpxEncoderErrorDetail {
118    pub vpx_error: VpxErrorDetail,
119}
120
121#[repr(C)]
122#[derive(Clone, Copy)]
123pub struct XmfVpxEncoderError {
124    pub code: XmfVpxEncoderErrorCode,
125    pub detail: XmfVpxEncoderErrorDetail,
126}
127
128#[repr(C)]
129#[derive(Debug, Clone, Copy)]
130pub enum XmfVpxPacketKind {
131    CodecCxFramePkt,
132    CodecStatsPkt,
133    CodecFpmbStatsPkt,
134    CodecPsnrPkt,
135    CodecCustomPkt = 256,
136}
137
138impl std::error::Error for XmfVpxDecoderError {}
139
140impl fmt::Display for XmfVpxDecoderError {
141    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
142        if matches!(self.code, XmfVpxDecoderErrorCode::VpxError) {
143            // SAFETY: The union detail.vpx_error is always valid when the error code is VpxError.
144            unsafe { write!(f, "VPX error: {:?}", self.detail.vpx_error.error_code) }
145        } else {
146            write!(f, "XMF VPX decoder error: {:?}", self.code)
147        }
148    }
149}
150
151impl Debug for XmfVpxDecoderError {
152    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
153        if matches!(self.code, XmfVpxDecoderErrorCode::VpxError) {
154            // SAFETY: The union detail.vpx_error is always valid when the error code is VpxError.
155            unsafe {
156                f.debug_struct("XmfVpxDecoderError")
157                    .field("code", &self.code)
158                    .field("detail", &self.detail.vpx_error.error_code)
159                    .finish()
160            }
161        } else {
162            f.debug_struct("XmfVpxDecoderError").field("code", &self.code).finish()
163        }
164    }
165}
166
167impl std::error::Error for XmfVpxEncoderError {}
168
169impl fmt::Display for XmfVpxEncoderError {
170    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
171        if matches!(self.code, XmfVpxEncoderErrorCode::VpxError) {
172            // SAFETY: The union detail.vpx_error is always valid when the error code is VpxErr:19or.
173            unsafe { write!(f, "VPX error: {:?}", self.detail.vpx_error.error_code) }
174        } else {
175            write!(f, "XMF VPX encoder error: {:?}", self.code)
176        }
177    }
178}
179
180impl Debug for XmfVpxEncoderError {
181    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
182        if matches!(self.code, XmfVpxEncoderErrorCode::VpxError) {
183            // SAFETY: The union detail.vpx_error is always valid when the error code is VpxError.
184            unsafe {
185                f.debug_struct("XmfVpxEncoderError")
186                    .field("code", &self.code)
187                    .field("detail", &self.detail.vpx_error.error_code)
188                    .finish()
189            }
190        } else {
191            f.debug_struct("XmfVpxEncoderError").field("code", &self.code).finish()
192        }
193    }
194}