Skip to content

Commit 5d8272b

Browse files
committed
update vpx
1 parent e8e5db6 commit 5d8272b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+14962
-151
lines changed

build.bat

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
@REM Download https://github.com/ShiftMediaProject/libvpx/releases/download/v1.10.0/libvpx_v1.10.0_msvc16.zip
2+
@REM and unzip into %HomeDrive%%HomePath%\libvpx_v1.10.0_msvc16
3+
set VPX_STATIC=1
4+
set VPX_VERSION=1.10.0
5+
set VPX_LIB_DIR=%HomeDrive%%HomePath%\libvpx_v1.10.0_msvc16\lib\x64
6+
set VPX_INCLUDE_DIR=%HomeDrive%%HomePath%\libvpx_v1.10.0_msvc16\include
7+
8+
@REM Download llvm from https://releases.llvm.org/download.html
9+
@REM # https://github.com/llvm/llvm-project/releases/download/llvmorg-18.1.8/clang+llvm-18.1.8-x86_64-pc-windows-msvc.tar.xz
10+
set LIBCLANG_PATH=%HomeDrive%%HomePath%\clang+llvm-18.1.8-x86_64-pc-windows-msvc\bin
11+
cargo build --release
12+

build.ps1

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Download https://github.com/ShiftMediaProject/libvpx/releases/download/v1.10.0/libvpx_v1.10.0_msvc16.zip
2+
# and unzip into %HomeDrive%%HomePath%\libvpx_v1.10.0_msvc16
3+
$env:VPX_STATIC="1"
4+
$env:VPX_VERSION="1.10.0"
5+
$env:VPX_LIB_DIR="$env:HomeDrive$env:HomePath\libvpx_v1.10.0_msvc16\lib\x64"
6+
$env:VPX_INCLUDE_DIR="$env:HomeDrive$env:HomePath\libvpx_v1.10.0_msvc16\include"
7+
8+
# Download llvm from https://releases.llvm.org/download.html
9+
# https://github.com/llvm/llvm-project/releases/download/llvmorg-18.1.8/clang+llvm-18.1.8-x86_64-pc-windows-msvc.tar.xz
10+
$env:LIBCLANG_PATH="$env:HomeDrive$env:HomePath\clang+llvm-18.1.8-x86_64-pc-windows-msvc\bin"
11+
cargo build --release

dsclient/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ panic = "abort"
1414
[dependencies]
1515
dscom = {path = "../dscom"}
1616

17-
flate2 = "1.0"
1817
fltk = { version = "1.5", features = ["fltk-bundled"] }
19-
rayon = "1.5"
18+
19+
vpx-codec = { path = "../libs/vpx-codec" }

dsclient/src/client.rs

Lines changed: 39 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use flate2::write::DeflateDecoder;
21
use fltk::button::Button;
3-
use fltk::draw;
42
use fltk::enums::Color;
53
use fltk::frame::Frame;
64
use fltk::input::Input;
@@ -12,6 +10,8 @@ use std::hash::Hasher;
1210
use std::io::Read;
1311
use std::io::Write;
1412
use std::net::TcpStream;
13+
use std::sync::atomic::AtomicI32;
14+
use std::sync::atomic::Ordering;
1515
use std::sync::Arc;
1616
use std::sync::RwLock;
1717

@@ -23,7 +23,6 @@ use fltk::prelude::GroupExt;
2323
use fltk::prelude::ImageExt;
2424
use fltk::prelude::WidgetBase;
2525
use fltk::prelude::WidgetExt;
26-
use rayon::prelude::*;
2726

2827
use crate::bitmap;
2928

@@ -40,7 +39,7 @@ pub fn app_run() {
4039
);
4140
wind.set_color(Color::from_rgb(255, 255, 255));
4241
let mut host_ipt = Input::new(80, 20, 200, 25, "HOST:");
43-
host_ipt.set_value("127.0.0.1:80");
42+
host_ipt.set_value("127.0.0.1:38971");
4443
let mut pwd_ipt = SecretInput::new(80, 50, 200, 25, "PASS:");
4544
pwd_ipt.set_value("diffscreen");
4645
let mut login_btn = Button::new(200, 80, 80, 40, "Login");
@@ -109,17 +108,22 @@ fn draw(host: String, pwd: String) {
109108
if let Err(_) = conn.read_exact(&mut meta) {
110109
return;
111110
}
112-
let w = (((meta[0] as u16) << 8) | meta[1] as u16) as i32;
113-
let h = (((meta[2] as u16) << 8) | meta[3] as u16) as i32;
111+
let iw = (((meta[0] as u16) << 8) | meta[1] as u16) as i32;
112+
let ih = (((meta[2] as u16) << 8) | meta[3] as u16) as i32;
114113

115-
let dlen = (w * h * 3) as usize;
114+
let wh = Arc::new((AtomicI32::new(iw), AtomicI32::new(ih)));
116115

117-
let work_buf = Arc::new(RwLock::new(vec![0u8; dlen]));
116+
117+
// let dlen = (w * h * 3) as usize;
118+
119+
let work_buf = Arc::new(RwLock::new((0usize, 0usize, Vec::<u8>::new())));
118120
let draw_work_buf = work_buf.clone();
119121
let mut hooked = false;
120122
let mut bmap = bitmap::Bitmap::new();
121123
let mut cmd_buf = [0u8; 5];
124+
let wh1 = wh.clone();
122125
frame.handle(move |f, ev| {
126+
let (w, h) = (wh1.0.load(Ordering::Relaxed), wh1.1.load(Ordering::Relaxed));
123127
match ev {
124128
Event::Enter => {
125129
// 进入窗口
@@ -215,20 +219,14 @@ fn draw(host: String, pwd: String) {
215219
}
216220
true
217221
});
218-
let _tool_str = Arc::new(RwLock::new(String::new()));
219-
let _tool_strc = _tool_str.clone();
220222
frame.draw(move |frame|{
221-
if let Ok(_buf) = draw_work_buf.read() {
223+
if let Ok(p) = draw_work_buf.read() {
222224
unsafe {
223225
if let Ok(mut image) =
224-
image::RgbImage::from_data2(&_buf, w, h, enums::ColorDepth::Rgb8 as i32, 0)
226+
image::RgbImage::from_data2(&p.2, p.0 as _, p.1 as _, enums::ColorDepth::Rgb8 as i32, 0)
225227
{
226228
image.scale(frame.width(), frame.height(), false, true);
227-
image.draw(frame.x(), frame.y(), frame.width(), frame.height());
228-
draw::set_color_rgb(0, 0, 0);
229-
if let Ok(a) = _tool_strc.read() {
230-
draw::draw_text(&a, frame.x() + frame.width() - 180, 20);
231-
}
229+
image.draw(frame.x(), frame.y(), frame.width(), frame.height());
232230
}
233231
}
234232
}
@@ -237,94 +235,46 @@ fn draw(host: String, pwd: String) {
237235
let (tx, rx) = app::channel::<Msg>();
238236

239237
std::thread::spawn(move || {
240-
let u = (w * h) as usize;
241-
let v = u + u/4;
242-
let mut yuv = Vec::<u8>::new();
243-
let mut _yuv = Vec::<u8>::new();
244238
let mut buf = Vec::<u8>::new();
239+
let fps = 30;
245240

246-
// FPS
247-
let mut last = std::time::Instant::now();
248-
let mut fps = 0u8;
249-
let mut fpscount = 0u8;
250-
// 流速
251-
let mut _length_all = 0usize;
252-
let mut _length_sum = 0usize;
253-
// 接收第一帧数据
254-
let mut header = [0u8; 3];
255-
if let Err(_) = conn.read_exact(&mut header) {
256-
return;
257-
}
258-
let recv_len = depack(&header);
259-
_length_sum += recv_len;
260-
261-
if buf.capacity() < recv_len {
262-
buf.resize(recv_len, 0u8);
263-
}
264-
if let Err(e) = conn.read_exact(&mut buf) {
265-
println!("error {}", e);
266-
return;
267-
}
268-
unsafe {
269-
yuv.set_len(0);
270-
}
271-
let mut d = DeflateDecoder::new(yuv);
272-
d.write_all(&buf).unwrap();
273-
yuv = d.reset(Vec::new()).unwrap();
241+
let ecfg = vpx_codec::decoder::Config {
242+
width: iw as _,
243+
height: ih as _,
244+
timebase: [1, (fps as i32) * 1000], // 120fps
245+
bitrate: 8192,
246+
codec: vpx_codec::decoder::VideoCodecId::VP8,
247+
};
274248

275-
if let Ok(mut _buf) = work_buf.write() {
276-
dscom::convert::i420_to_rgb(w as usize, h as usize, &yuv[..u], &yuv[u..v], &yuv[v..], &mut _buf);
277-
}
278-
(_yuv, yuv) = (yuv, _yuv);
279-
tx.send(Msg::Draw);
249+
let mut dec = vpx_codec::decoder::Decoder::new(ecfg).unwrap();
280250

281251
loop {
252+
let mut header = [0u8; 3];
282253
if let Err(_) = conn.read_exact(&mut header) {
283254
return;
284255
}
285256
let recv_len = depack(&header);
286-
_length_sum += recv_len;
287257

288-
if buf.capacity() < recv_len {
289-
buf.resize(recv_len, 0u8);
290-
} else {
291-
unsafe {
292-
buf.set_len(recv_len);
293-
}
294-
}
295-
if let Err(_) = conn.read_exact(&mut buf) {
258+
buf.resize(recv_len, 0u8);
259+
if let Err(e) = conn.read_exact(&mut buf) {
260+
println!("error {}", e);
296261
return;
297262
}
298-
unsafe {
299-
yuv.set_len(0);
300-
}
301-
d.write_all(&buf).unwrap();
302-
yuv = d.reset(yuv).unwrap();
303-
304-
yuv.par_iter_mut().zip(_yuv.par_iter()).for_each(|(a, b)| {
305-
*a = *b ^ *a;
306-
});
307263

308-
if let Ok(mut _buf) = work_buf.write() {
309-
dscom::convert::i420_to_rgb(w as usize, h as usize, &yuv[..u], &yuv[u..v], &yuv[v..], &mut _buf);
310-
}
311-
(_yuv, yuv) = (yuv, _yuv);
312-
{
313-
let cur = std::time::Instant::now();
314-
let dur = cur.duration_since(last);
315-
fpscount += 1;
316-
if dur.as_millis() >= 1000 {
317-
last = cur;
318-
_length_all = _length_sum;
319-
if let Ok(mut a) = _tool_str.write() {
320-
*a = format!("FPS:{:2} | Rate:{:>6}KB/s", fps, _length_all / 1024);
264+
if let Ok(pkgs) = dec.decode(&buf) {
265+
for ele in pkgs {
266+
let (y, u, v) = ele.data();
267+
if let Ok(mut p) = work_buf.write() {
268+
p.0 = ele.width();
269+
p.1 = ele.height();
270+
wh.0.store(ele.width() as _, Ordering::Relaxed);
271+
wh.1.store(ele.height() as _, Ordering::Relaxed);
272+
p.2.resize(ele.width() * ele.height() * 3, 0u8);
273+
dscom::convert::i420_to_rgb(ele.width(), ele.height(), y, u, v, &mut p.2);
321274
}
322-
fps = fpscount;
323-
fpscount = 0;
324-
_length_sum = 0;
275+
tx.send(Msg::Draw);
325276
}
326277
}
327-
tx.send(Msg::Draw);
328278
}
329279
});
330280
while app::wait() {

dscom/src/convert.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
pub fn bgra_to_i420(width: usize, height: usize, src: &[u8], dest: &mut Vec<u8>) {
22
let stride = src.len() / height;
3-
unsafe {
4-
dest.set_len(0);
5-
}
3+
4+
dest.clear();
65

76
for y in 0..height {
87
for x in 0..width {
@@ -48,6 +47,7 @@ fn clamp(x: i32) -> u8 {
4847
x.min(255).max(0) as u8
4948
}
5049

50+
#[allow(dead_code)]
5151
pub fn i420_to_rgb(width: usize, height: usize, sy: &[u8], su: &[u8], sv: &[u8], dest: &mut [u8]) {
5252
let uvw = width >> 1;
5353
for i in 0..height {
@@ -65,6 +65,8 @@ pub fn i420_to_rgb(width: usize, height: usize, sy: &[u8], su: &[u8], sv: &[u8],
6565
dest[rgbstart] = clamp(y + (v * 359 >> 8));
6666
dest[rgbstart + 1] = clamp(y - (u * 88 >> 8) - (v * 182 >> 8));
6767
dest[rgbstart + 2] = clamp(y + (u * 453 >> 8));
68+
6869
}
6970
}
70-
}
71+
72+
}

dsserver/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ edition = "2021"
88
[dependencies]
99
dscom = {path = "../dscom"}
1010

11-
flate2 = "1.0"
1211
scrap = "0.5"
1312
enigo = "0.1.3"
14-
rayon = "1.5"
13+
14+
vpx-codec = { path = "../libs/vpx-codec" }

dsserver/src/convert.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
pub fn bgra_to_i420(width: usize, height: usize, src: &[u8], dest: &mut Vec<u8>) {
2+
let stride = src.len() / height;
3+
4+
dest.clear();
5+
6+
for y in 0..height {
7+
for x in 0..width {
8+
let o = y * stride + 4 * x;
9+
10+
let b = src[o] as i32;
11+
let g = src[o + 1] as i32;
12+
let r = src[o + 2] as i32;
13+
14+
let y = (66 * r + 129 * g + 25 * b + 128) / 256 + 16;
15+
dest.push(clamp(y));
16+
}
17+
}
18+
19+
for y in (0..height).step_by(2) {
20+
for x in (0..width).step_by(2) {
21+
let o = y * stride + 4 * x;
22+
23+
let b = src[o] as i32;
24+
let g = src[o + 1] as i32;
25+
let r = src[o + 2] as i32;
26+
27+
let u = (-38 * r - 74 * g + 112 * b + 128) / 256 + 128;
28+
dest.push(clamp(u));
29+
}
30+
}
31+
32+
for y in (0..height).step_by(2) {
33+
for x in (0..width).step_by(2) {
34+
let o = y * stride + 4 * x;
35+
36+
let b = src[o] as i32;
37+
let g = src[o + 1] as i32;
38+
let r = src[o + 2] as i32;
39+
40+
let v = (112 * r - 94 * g - 18 * b + 128) / 256 + 128;
41+
dest.push(clamp(v));
42+
}
43+
}
44+
}
45+
46+
fn clamp(x: i32) -> u8 {
47+
x.min(255).max(0) as u8
48+
}
49+
50+
#[allow(dead_code)]
51+
pub fn i420_to_rgb(width: usize, height: usize, sy: &[u8], su: &[u8], sv: &[u8], dest: &mut [u8]) {
52+
let uvw = width >> 1;
53+
for i in 0..height {
54+
let sw = i * width;
55+
let t = (i >> 1) * uvw;
56+
for j in 0..width {
57+
let mut rgbstart = sw + j;
58+
let uvi = t + (j >> 1);
59+
60+
let y = sy[rgbstart] as i32;
61+
let u = su[uvi] as i32 - 128;
62+
let v = sv[uvi] as i32 - 128;
63+
64+
rgbstart *= 3;
65+
dest[rgbstart] = clamp(y + (v * 359 >> 8));
66+
dest[rgbstart + 1] = clamp(y - (u * 88 >> 8) - (v * 182 >> 8));
67+
dest[rgbstart + 2] = clamp(y + (u * 453 >> 8));
68+
69+
}
70+
}
71+
72+
}

dsserver/src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
mod key_mouse;
22
mod screen;
33
mod server;
4+
mod convert;
45
fn main() {
56
let args: Vec<String> = std::env::args().collect();
67

@@ -11,7 +12,7 @@ fn main() {
1112
}
1213

1314
// defalut port
14-
let mut port = 80;
15+
let mut port = 38971;
1516
if args.len() >= 3 {
1617
port = args[2].parse::<u16>().unwrap();
1718
}

0 commit comments

Comments
 (0)