roxify 1.7.5 → 1.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Cargo.toml +82 -0
- package/dist/cli.js +1 -1
- package/dist/roxify_native.exe +0 -0
- package/native/archive.rs +176 -0
- package/native/audio.rs +151 -0
- package/native/bwt.rs +100 -0
- package/native/context_mixing.rs +120 -0
- package/native/core.rs +293 -0
- package/native/crypto.rs +119 -0
- package/native/encoder.rs +640 -0
- package/native/gpu.rs +116 -0
- package/native/hybrid.rs +162 -0
- package/native/image_utils.rs +77 -0
- package/native/lib.rs +464 -0
- package/native/main.rs +461 -0
- package/native/packer.rs +444 -0
- package/native/png_utils.rs +192 -0
- package/native/pool.rs +101 -0
- package/native/progress.rs +43 -0
- package/native/rans.rs +149 -0
- package/native/reconstitution.rs +511 -0
- package/package.json +6 -1
- package/scripts/postinstall.cjs +101 -0
package/native/pool.rs
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
use anyhow::Result;
|
|
2
|
+
use parking_lot::RwLock;
|
|
3
|
+
use std::sync::Arc;
|
|
4
|
+
|
|
5
|
+
pub struct ReusableBuffer {
|
|
6
|
+
data: Vec<u8>,
|
|
7
|
+
capacity: usize,
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
impl ReusableBuffer {
|
|
11
|
+
pub fn new(capacity: usize) -> Self {
|
|
12
|
+
ReusableBuffer {
|
|
13
|
+
data: vec![0u8; capacity],
|
|
14
|
+
capacity,
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
pub fn as_mut_slice(&mut self) -> &mut [u8] {
|
|
19
|
+
&mut self.data
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
pub fn as_slice(&self) -> &[u8] {
|
|
23
|
+
&self.data
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
pub fn resize(&mut self, new_size: usize) -> Result<()> {
|
|
27
|
+
if new_size > self.capacity {
|
|
28
|
+
return Err(anyhow::anyhow!("Buffer overflow: {} > {}", new_size, self.capacity));
|
|
29
|
+
}
|
|
30
|
+
self.data.truncate(new_size);
|
|
31
|
+
Ok(())
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
pub fn clear(&mut self) {
|
|
35
|
+
self.data.clear();
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
pub struct BufferPool {
|
|
40
|
+
buffers: Arc<RwLock<Vec<Arc<RwLock<ReusableBuffer>>>>>,
|
|
41
|
+
default_capacity: usize,
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
impl BufferPool {
|
|
45
|
+
pub fn new(pool_size: usize, capacity: usize) -> Self {
|
|
46
|
+
let mut buffers = Vec::with_capacity(pool_size);
|
|
47
|
+
for _ in 0..pool_size {
|
|
48
|
+
buffers.push(Arc::new(RwLock::new(ReusableBuffer::new(capacity))));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
BufferPool {
|
|
52
|
+
buffers: Arc::new(RwLock::new(buffers)),
|
|
53
|
+
default_capacity: capacity,
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
pub fn acquire(&self) -> Arc<RwLock<ReusableBuffer>> {
|
|
58
|
+
let mut pool = self.buffers.write();
|
|
59
|
+
if let Some(buf) = pool.pop() {
|
|
60
|
+
buf
|
|
61
|
+
} else {
|
|
62
|
+
Arc::new(RwLock::new(ReusableBuffer::new(self.default_capacity)))
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
pub fn release(&self, buf: Arc<RwLock<ReusableBuffer>>) {
|
|
67
|
+
buf.write().clear();
|
|
68
|
+
let mut pool = self.buffers.write();
|
|
69
|
+
if pool.len() < 16 {
|
|
70
|
+
pool.push(buf);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
pub struct ZeroCopyBuffer {
|
|
76
|
+
ptr: *const u8,
|
|
77
|
+
len: usize,
|
|
78
|
+
owned: bool,
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
impl ZeroCopyBuffer {
|
|
82
|
+
pub fn from_slice(data: &[u8]) -> Self {
|
|
83
|
+
ZeroCopyBuffer {
|
|
84
|
+
ptr: data.as_ptr(),
|
|
85
|
+
len: data.len(),
|
|
86
|
+
owned: false,
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
pub fn as_slice(&self) -> &[u8] {
|
|
91
|
+
unsafe { std::slice::from_raw_parts(self.ptr, self.len) }
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
pub fn len(&self) -> usize {
|
|
95
|
+
self.len
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
pub fn is_empty(&self) -> bool {
|
|
99
|
+
self.len == 0
|
|
100
|
+
}
|
|
101
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
use parking_lot::Mutex;
|
|
2
|
+
use std::sync::Arc;
|
|
3
|
+
|
|
4
|
+
pub struct ProgressBar {
|
|
5
|
+
total: u64,
|
|
6
|
+
current: Arc<Mutex<u64>>,
|
|
7
|
+
message: Arc<Mutex<String>>,
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
impl ProgressBar {
|
|
11
|
+
pub fn new(total: u64) -> Self {
|
|
12
|
+
Self {
|
|
13
|
+
total,
|
|
14
|
+
current: Arc::new(Mutex::new(0)),
|
|
15
|
+
message: Arc::new(Mutex::new(String::new())),
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
pub fn inc(&self, delta: u64) {
|
|
20
|
+
let mut current = self.current.lock();
|
|
21
|
+
*current += delta;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
pub fn set(&self, value: u64) {
|
|
25
|
+
let mut current = self.current.lock();
|
|
26
|
+
*current = value;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
pub fn set_message(&self, msg: String) {
|
|
30
|
+
let mut message = self.message.lock();
|
|
31
|
+
*message = msg;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
pub fn get_progress(&self) -> (u64, u64) {
|
|
35
|
+
let current = *self.current.lock();
|
|
36
|
+
(current, self.total)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
pub fn finish(&self) {
|
|
40
|
+
let mut current = self.current.lock();
|
|
41
|
+
*current = self.total;
|
|
42
|
+
}
|
|
43
|
+
}
|
package/native/rans.rs
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
use anyhow::Result;
|
|
2
|
+
|
|
3
|
+
const RANS_L: u32 = 1 << 31;
|
|
4
|
+
const RANS_M: u32 = 1 << 16;
|
|
5
|
+
|
|
6
|
+
#[derive(Clone, Debug)]
|
|
7
|
+
pub struct Symbol {
|
|
8
|
+
pub start: u32,
|
|
9
|
+
pub freq: u32,
|
|
10
|
+
pub scale_bits: u32,
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
pub struct RansEncoder {
|
|
14
|
+
state: u32,
|
|
15
|
+
output: Vec<u8>,
|
|
16
|
+
symbols: Vec<Symbol>,
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
impl RansEncoder {
|
|
20
|
+
pub fn new(symbols: Vec<Symbol>) -> Self {
|
|
21
|
+
RansEncoder {
|
|
22
|
+
state: RANS_L,
|
|
23
|
+
output: Vec::new(),
|
|
24
|
+
symbols,
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
pub fn encode(&mut self, symbol_idx: usize) -> Result<()> {
|
|
29
|
+
if symbol_idx >= self.symbols.len() {
|
|
30
|
+
return Err(anyhow::anyhow!("Symbol index out of bounds"));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
let sym = &self.symbols[symbol_idx];
|
|
34
|
+
|
|
35
|
+
while self.state >= (sym.freq << 16) {
|
|
36
|
+
self.output.push((self.state & 0xFF) as u8);
|
|
37
|
+
self.state >>= 8;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
let x = ((self.state / sym.freq) << sym.scale_bits) + sym.start;
|
|
41
|
+
let y = self.state % sym.freq;
|
|
42
|
+
self.state = x + y;
|
|
43
|
+
|
|
44
|
+
Ok(())
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
pub fn finish(mut self) -> Vec<u8> {
|
|
48
|
+
while self.state > 0 {
|
|
49
|
+
self.output.push((self.state & 0xFF) as u8);
|
|
50
|
+
self.state >>= 8;
|
|
51
|
+
}
|
|
52
|
+
self.output.reverse();
|
|
53
|
+
self.output
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
pub struct RansDecoder {
|
|
58
|
+
state: u32,
|
|
59
|
+
data: Vec<u8>,
|
|
60
|
+
pos: usize,
|
|
61
|
+
symbols: Vec<Symbol>,
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
impl RansDecoder {
|
|
65
|
+
pub fn new(data: Vec<u8>, symbols: Vec<Symbol>) -> Self {
|
|
66
|
+
let mut decoder = RansDecoder {
|
|
67
|
+
state: RANS_L,
|
|
68
|
+
data,
|
|
69
|
+
pos: 0,
|
|
70
|
+
symbols,
|
|
71
|
+
};
|
|
72
|
+
decoder.refill();
|
|
73
|
+
decoder
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
fn refill(&mut self) {
|
|
77
|
+
while self.state < RANS_L && self.pos < self.data.len() {
|
|
78
|
+
self.state = (self.state << 8) | (self.data[self.pos] as u32);
|
|
79
|
+
self.pos += 1;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
pub fn decode(&mut self) -> Result<usize> {
|
|
84
|
+
let x = self.state & 0xFFFF;
|
|
85
|
+
|
|
86
|
+
let mut sym_idx = 0;
|
|
87
|
+
for (i, sym) in self.symbols.iter().enumerate() {
|
|
88
|
+
if x >= sym.start && x < sym.start + sym.freq {
|
|
89
|
+
sym_idx = i;
|
|
90
|
+
break;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
let sym = &self.symbols[sym_idx];
|
|
95
|
+
let q = self.state / sym.freq;
|
|
96
|
+
let r = self.state % sym.freq;
|
|
97
|
+
|
|
98
|
+
self.state = (q << sym.scale_bits) + (r + sym.start);
|
|
99
|
+
self.refill();
|
|
100
|
+
|
|
101
|
+
Ok(sym_idx)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
pub fn is_finished(&self) -> bool {
|
|
105
|
+
self.state == RANS_L && self.pos >= self.data.len()
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
pub fn build_symbols_from_frequencies(freqs: &[u32]) -> Vec<Symbol> {
|
|
110
|
+
let total: u32 = freqs.iter().sum();
|
|
111
|
+
if total == 0 {
|
|
112
|
+
return Vec::new();
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
let scale_bits = 16 - (total.leading_zeros() - 16);
|
|
116
|
+
let mut symbols = Vec::new();
|
|
117
|
+
let mut start = 0u32;
|
|
118
|
+
|
|
119
|
+
for freq in freqs {
|
|
120
|
+
if *freq > 0 {
|
|
121
|
+
let scaled = ((*freq as u64) << scale_bits) / (total as u64);
|
|
122
|
+
symbols.push(Symbol {
|
|
123
|
+
start,
|
|
124
|
+
freq: *freq,
|
|
125
|
+
scale_bits,
|
|
126
|
+
});
|
|
127
|
+
start += scaled as u32;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
symbols
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
pub fn estimate_entropy(freqs: &[u32]) -> f64 {
|
|
135
|
+
let total: u32 = freqs.iter().sum();
|
|
136
|
+
if total == 0 {
|
|
137
|
+
return 0.0;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
let total_f = total as f64;
|
|
141
|
+
freqs
|
|
142
|
+
.iter()
|
|
143
|
+
.filter(|&&f| f > 0)
|
|
144
|
+
.map(|&f| {
|
|
145
|
+
let p = (f as f64) / total_f;
|
|
146
|
+
-p * p.log2()
|
|
147
|
+
})
|
|
148
|
+
.sum()
|
|
149
|
+
}
|