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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
use crate::error::NativeErrorExt;
use crate::Error;
use openh264_sys2::{
EVideoFormatType, ISVCDecoder, ISVCDecoderVtbl, SBufferInfo, SDecodingParam, SParserBsInfo, SSysMEMBuffer, WelsCreateDecoder,
WelsDestroyDecoder, DECODER_OPTION, DECODING_STATE,
};
use std::os::raw::{c_int, c_long, c_uchar, c_void};
use std::ptr::{addr_of_mut, null, null_mut};
#[rustfmt::skip]
#[allow(non_snake_case)]
#[derive(Debug)]
struct DecoderRawAPI {
decoder_ptr: *mut *const ISVCDecoderVtbl,
initialize: unsafe extern "C" fn(arg1: *mut ISVCDecoder, pParam: *const SDecodingParam) -> c_long,
uninitialize: unsafe extern "C" fn(arg1: *mut ISVCDecoder) -> c_long,
decode_frame: unsafe extern "C" fn(arg1: *mut ISVCDecoder, pSrc: *const c_uchar, iSrcLen: c_int, ppDst: *mut *mut c_uchar, pStride: *mut c_int, iWidth: *mut c_int, iHeight: *mut c_int) -> DECODING_STATE,
decode_frame_no_delay: unsafe extern "C" fn(arg1: *mut ISVCDecoder, pSrc: *const c_uchar, iSrcLen: c_int, ppDst: *mut *mut c_uchar, pDstInfo: *mut SBufferInfo) -> DECODING_STATE,
decode_frame2: unsafe extern "C" fn(arg1: *mut ISVCDecoder, pSrc: *const c_uchar, iSrcLen: c_int, ppDst: *mut *mut c_uchar, pDstInfo: *mut SBufferInfo) -> DECODING_STATE,
flush_frame: unsafe extern "C" fn(arg1: *mut ISVCDecoder, ppDst: *mut *mut c_uchar, pDstInfo: *mut SBufferInfo) -> DECODING_STATE,
decode_parser: unsafe extern "C" fn(arg1: *mut ISVCDecoder, pSrc: *const c_uchar, iSrcLen: c_int, pDstInfo: *mut SParserBsInfo) -> DECODING_STATE,
decode_frame_ex: unsafe extern "C" fn(arg1: *mut ISVCDecoder, pSrc: *const c_uchar, iSrcLen: c_int, pDst: *mut c_uchar, iDstStride: c_int, iDstLen: *mut c_int, iWidth: *mut c_int, iHeight: *mut c_int, iColorFormat: *mut c_int) -> DECODING_STATE,
set_option: unsafe extern "C" fn(arg1: *mut ISVCDecoder, eOptionId: DECODER_OPTION, pOption: *mut c_void) -> c_long,
get_option: unsafe extern "C" fn(arg1: *mut ISVCDecoder, eOptionId: DECODER_OPTION, pOption: *mut c_void) -> c_long,
}
#[rustfmt::skip]
#[allow(clippy::too_many_arguments)]
#[allow(non_snake_case)]
#[allow(unused)]
impl DecoderRawAPI {
fn new() -> Result<Self, Error> {
unsafe {
let mut decoder_ptr = null::<ISVCDecoderVtbl>() as *mut *const ISVCDecoderVtbl;
WelsCreateDecoder(&mut decoder_ptr as *mut *mut *const ISVCDecoderVtbl).ok()?;
let e = Error::msg("VTable missing function.");
Ok(DecoderRawAPI {
decoder_ptr,
initialize: (*(*decoder_ptr)).Initialize.ok_or(e)?,
uninitialize: (*(*decoder_ptr)).Uninitialize.ok_or(e)?,
decode_frame: (*(*decoder_ptr)).DecodeFrame.ok_or(e)?,
decode_frame_no_delay: (*(*decoder_ptr)).DecodeFrameNoDelay.ok_or(e)?,
decode_frame2: (*(*decoder_ptr)).DecodeFrame2.ok_or(e)?,
flush_frame: (*(*decoder_ptr)).FlushFrame.ok_or(e)?,
decode_parser: (*(*decoder_ptr)).DecodeParser.ok_or(e)?,
decode_frame_ex: (*(*decoder_ptr)).DecodeFrameEx.ok_or(e)?,
set_option: (*(*decoder_ptr)).SetOption.ok_or(e)?,
get_option: (*(*decoder_ptr)).GetOption.ok_or(e)?,
})
}
}
unsafe fn initialize(&self, pParam: *const SDecodingParam) -> c_long {
(self.initialize)(self.decoder_ptr, pParam)
}
unsafe fn uninitialize(&self, ) -> c_long {
(self.uninitialize)(self.decoder_ptr)
}
unsafe fn decode_frame(&self, Src: *const c_uchar, iSrcLen: c_int, ppDst: *mut *mut c_uchar, pStride: *mut c_int, iWidth: *mut c_int, iHeight: *mut c_int) -> DECODING_STATE {
(self.decode_frame)(self.decoder_ptr, Src, iSrcLen, ppDst, pStride, iWidth, iHeight)
}
unsafe fn decode_frame_no_delay(&self, pSrc: *const c_uchar, iSrcLen: c_int, ppDst: *mut *mut c_uchar, pDstInfo: *mut SBufferInfo) -> DECODING_STATE {
(self.decode_frame_no_delay)(self.decoder_ptr, pSrc, iSrcLen, ppDst, pDstInfo)
}
unsafe fn decode_frame2(&self, pSrc: *const c_uchar, iSrcLen: c_int, ppDst: *mut *mut c_uchar, pDstInfo: *mut SBufferInfo) -> DECODING_STATE {
(self.decode_frame2)(self.decoder_ptr, pSrc, iSrcLen, ppDst, pDstInfo)
}
unsafe fn flush_frame(&self, ppDst: *mut *mut c_uchar, pDstInfo: *mut SBufferInfo) -> DECODING_STATE {
(self.flush_frame)(self.decoder_ptr, ppDst, pDstInfo)
}
unsafe fn decode_parser(&self, pSrc: *const c_uchar, iSrcLen: c_int, pDstInfo: *mut SParserBsInfo) -> DECODING_STATE {
(self.decode_parser)(self.decoder_ptr, pSrc, iSrcLen, pDstInfo)
}
unsafe fn decode_frame_ex(&self, pSrc: *const c_uchar, iSrcLen: c_int, pDst: *mut c_uchar, iDstStride: c_int, iDstLen: *mut c_int, iWidth: *mut c_int, iHeight: *mut c_int, iColorFormat: *mut c_int) -> DECODING_STATE {
(self.decode_frame_ex)(self.decoder_ptr, pSrc, iSrcLen, pDst, iDstStride, iDstLen, iWidth, iHeight, iColorFormat)
}
unsafe fn set_option(&self, eOptionId: DECODER_OPTION, pOption: *mut c_void) -> c_long {
(self.set_option)(self.decoder_ptr, eOptionId, pOption)
}
unsafe fn get_option(&self, eOptionId: DECODER_OPTION, pOption: *mut c_void) -> c_long {
(self.get_option)(self.decoder_ptr, eOptionId, pOption)
}
}
impl Drop for DecoderRawAPI {
fn drop(&mut self) {
unsafe {
WelsDestroyDecoder(self.decoder_ptr);
}
}
}
#[derive(Default, Copy, Clone, Debug)]
pub struct DecoderConfig {
params: SDecodingParam,
num_threads: i32,
}
impl DecoderConfig {
pub fn new() -> Self {
Self::default()
}
pub unsafe fn num_threads(mut self, num_threads: u32) -> Self {
self.num_threads = num_threads as i32;
self
}
}
#[derive(Debug)]
pub struct Decoder {
raw_api: DecoderRawAPI,
}
impl Decoder {
pub fn new() -> Result<Self, Error> {
Self::with_config(DecoderConfig::new())
}
pub fn with_config(mut config: DecoderConfig) -> Result<Self, Error> {
let raw = DecoderRawAPI::new()?;
unsafe {
raw.initialize(&config.params).ok()?;
raw.set_option(
DECODER_OPTION::DECODER_OPTION_NUM_OF_THREADS,
addr_of_mut!(config.num_threads).cast(),
)
.ok()?;
};
Ok(Self { raw_api: raw })
}
pub fn decode_no_delay(&mut self, packet: &[u8]) -> Result<DecodedYUV, Error> {
let mut dst = [null_mut(); 3];
let mut buffer_info = SBufferInfo::default();
unsafe {
self.raw_api
.decode_frame_no_delay(packet.as_ptr(), packet.len() as i32, &mut dst as *mut _, &mut buffer_info)
.ok()?;
if !buffer_info.iBufferStatus == 1 {
return Err(Error::msg("Buffer status not valid"));
}
let info = buffer_info.UsrData.sSystemBuffer;
let y = std::slice::from_raw_parts(dst[0], (info.iHeight * info.iStride[0]) as usize);
let u = std::slice::from_raw_parts(dst[1], (info.iHeight * info.iStride[1] / 2) as usize);
let v = std::slice::from_raw_parts(dst[2], (info.iHeight * info.iStride[1] / 2) as usize);
Ok(DecodedYUV { info, y, u, v })
}
}
}
impl Drop for Decoder {
fn drop(&mut self) {
unsafe {
self.raw_api.uninitialize();
}
}
}
pub struct DecodedYUV<'a> {
info: SSysMEMBuffer,
y: &'a [u8],
u: &'a [u8],
v: &'a [u8],
}
impl<'a> DecodedYUV<'a> {
pub fn y_with_stride(&self) -> &'a [u8] {
self.y
}
pub fn u_with_stride(&self) -> &'a [u8] {
self.u
}
pub fn v_with_stride(&self) -> &'a [u8] {
self.v
}
pub fn dimension_rgb(&self) -> (usize, usize) {
(self.info.iWidth as usize, self.info.iHeight as usize)
}
pub fn dimension_y(&self) -> (usize, usize) {
(self.info.iWidth as usize, self.info.iHeight as usize)
}
pub fn dimension_u(&self) -> (usize, usize) {
(self.info.iWidth as usize / 2, self.info.iHeight as usize / 2)
}
pub fn dimension_v(&self) -> (usize, usize) {
(self.info.iWidth as usize / 2, self.info.iHeight as usize / 2)
}
pub fn strides_yuv(&self) -> (usize, usize, usize) {
(
self.info.iStride[0] as usize,
self.info.iStride[1] as usize,
self.info.iStride[1] as usize,
)
}
pub fn write_rgb8(&self, target: &mut [u8]) -> Result<(), Error> {
let dim = self.dimension_rgb();
let strides = self.strides_yuv();
assert_eq!(self.info.iFormat, EVideoFormatType::videoFormatI420 as i32);
if target.len() != (dim.0 * dim.1 * 3) as usize {
return Err(Error::msg("Target RGB8 array does not match image dimensions."));
}
for y in 0..dim.1 {
for x in 0..dim.0 {
let base_tgt = (y * dim.0 + x) * 3;
let base_y = y * strides.0 + x;
let base_u = (y / 2 * strides.1) + (x / 2);
let base_v = (y / 2 * strides.2) + (x / 2);
let rgb_pixel = &mut target[base_tgt..base_tgt + 3];
let y = self.y[base_y] as f32;
let u = self.u[base_u] as f32;
let v = self.v[base_v] as f32;
rgb_pixel[0] = (y + 1.402 * (v - 128.0)) as u8;
rgb_pixel[1] = (y - 0.344 * (u - 128.0) - 0.714 * (v - 128.0)) as u8;
rgb_pixel[2] = (y + 1.772 * (u - 128.0)) as u8;
}
}
Ok(())
}
}