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
//! # lamco-video
//!
//! Video frame processing and RDP bitmap conversion for Wayland screen capture.
//!
//! This crate is part of the [lamco-wayland](https://github.com/lamco-admin/lamco-wayland)
//! workspace and is designed to work with [`lamco-pipewire`](https://crates.io/crates/lamco-pipewire)
//! for video frame processing.
//!
//! # Features
//!
//! - **Frame Processing Pipeline**: Configurable video frame processing with rate limiting
//! - **RDP Bitmap Conversion**: Convert PipeWire frames to RDP-ready bitmap format
//! - **Damage Region Tracking**: Optimize updates by only sending changed regions
//! - **Buffer Pooling**: Efficient memory management with reusable buffers
//! - **Priority-Based Dispatch**: Multi-stream coordination with backpressure handling
//! - **SIMD Optimization**: Automatic use of SIMD instructions where available
//!
//! # Requirements
//!
//! This crate requires:
//! - **Linux** with a Wayland compositor
//! - **Rust 1.77+**
//! - **lamco-pipewire** for frame capture
//!
//! # Quick Start
//!
//! ```rust,ignore
//! use lamco_video::{FrameProcessor, ProcessorConfig, BitmapConverter};
//! use lamco_pipewire::VideoFrame;
//! use tokio::sync::mpsc;
//!
//! // Create frame processor
//! let config = ProcessorConfig::default();
//! let processor = std::sync::Arc::new(FrameProcessor::new(config, 1920, 1080));
//!
//! // Create channels
//! let (input_tx, input_rx) = mpsc::channel(30);
//! let (output_tx, mut output_rx) = mpsc::channel(30);
//!
//! // Start processor
//! let processor_clone = processor.clone();
//! tokio::spawn(async move {
//! processor_clone.start(input_rx, output_tx).await
//! });
//!
//! // Send frames from lamco-pipewire to processor
//! // Receive processed bitmap updates
//! while let Some(bitmap_update) = output_rx.recv().await {
//! for rect in &bitmap_update.rectangles {
//! println!("Update region: {:?}", rect.rectangle);
//! }
//! }
//! ```
//!
//! # Architecture
//!
//! The processing pipeline:
//!
//! ```text
//! ┌────────────────────┐
//! │ lamco-pipewire │
//! │ (VideoFrame) │
//! └─────────┬──────────┘
//! │
//! ▼
//! ┌────────────────────┐
//! │ FrameDispatcher │ ◄── Multi-stream routing
//! │ (priority queue) │ Backpressure handling
//! └─────────┬──────────┘
//! │
//! ▼
//! ┌────────────────────┐
//! │ FrameProcessor │ ◄── Frame rate limiting
//! │ (rate control) │ Age-based dropping
//! └─────────┬──────────┘
//! │
//! ▼
//! ┌────────────────────┐
//! │ BitmapConverter │ ◄── Pixel format conversion
//! │ (format conv) │ Damage region tracking
//! └─────────┬──────────┘ Buffer pooling
//! │
//! ▼
//! ┌────────────────────┐
//! │ BitmapUpdate │ ◄── RDP-ready rectangles
//! │ (RDP output) │
//! └────────────────────┘
//! ```
//!
//! # Configuration
//!
//! ## Processor Configuration
//!
//! ```rust
//! use lamco_video::ProcessorConfig;
//!
//! let config = ProcessorConfig {
//! target_fps: 60, // Target frame rate
//! max_queue_depth: 30, // Max frames in queue before dropping
//! adaptive_quality: true, // Enable adaptive quality
//! damage_threshold: 0.05, // Minimum damage area to process (5%)
//! drop_on_full_queue: true, // Drop frames when queue is full
//! enable_metrics: true, // Enable statistics collection
//! };
//! ```
//!
//! ## Dispatcher Configuration
//!
//! ```rust
//! use lamco_video::DispatcherConfig;
//!
//! let config = DispatcherConfig {
//! channel_size: 30, // Buffer size per stream
//! priority_dispatch: true, // Enable priority-based dispatch
//! max_frame_age_ms: 150, // Drop frames older than 150ms
//! enable_backpressure: true, // Enable backpressure handling
//! high_water_mark: 0.8, // Trigger backpressure at 80%
//! low_water_mark: 0.5, // Release backpressure at 50%
//! load_balancing: true, // Enable load balancing
//! };
//! ```
//!
//! # RDP Pixel Formats
//!
//! The converter supports these RDP-compatible output formats:
//!
//! | Format | BPP | Description |
//! |--------|-----|-------------|
//! | BgrX32 | 4 | 32-bit BGRX (most common) |
//! | Bgr24 | 3 | 24-bit BGR |
//! | Rgb16 | 2 | 16-bit RGB 5:6:5 |
//! | Rgb15 | 2 | 15-bit RGB 5:5:5 |
//!
//! # Performance
//!
//! Typical performance on modern hardware:
//!
//! - **Conversion latency**: < 1ms per frame (1080p)
//! - **Memory usage**: < 50MB (with buffer pooling)
//! - **Throughput**: > 200 MB/s (with SIMD)
//! - **Frame rates**: Tested up to 144Hz
//!
//! # Cargo Features
//!
//! ```toml
//! [dependencies]
//! lamco-video = { version = "0.1", features = ["full"] }
//! ```
//!
//! | Feature | Default | Description |
//! |---------|---------|-------------|
//! | `damage` | No | Full damage region tracking |
//! | `full` | No | All features enabled |
// =============================================================================
// CORE MODULES
// =============================================================================
// =============================================================================
// RE-EXPORTS - PRIMARY API
// =============================================================================
// Converter types
pub use ;
// Dispatcher types
pub use ;
// Processor types
pub use ;
// =============================================================================
// CRATE-LEVEL ITEMS
// =============================================================================
/// Crate version
pub const VERSION: &str = env!;
/// Recommended frame queue size for a given refresh rate
///
/// Returns the recommended channel buffer size to hold approximately
/// 500ms of frames, capped at 72 frames.
///
/// # Arguments
///
/// * `refresh_rate` - Monitor refresh rate in Hz
///
/// # Returns
///
/// Recommended channel buffer size (15-72)
/// Calculate RDP-compatible stride for a given width and format
///
/// RDP requires stride aligned to 64 bytes for optimal performance.
///
/// # Arguments
///
/// * `width` - Image width in pixels
/// * `format` - Target RDP pixel format
///
/// # Returns
///
/// Aligned stride in bytes