oximedia-scaling 0.1.8

Professional video scaling operations for OxiMedia
Documentation
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
//! Lightweight neural network 2×/4× upscaling for `oximedia-scaling`.
//!
//! Implements an ESPCN-style (Efficient Sub-Pixel Convolutional Neural Network)
//! super-resolution pipeline using only patent-free techniques.  The network
//! performs all computation in the low-resolution domain and uses a sub-pixel
//! shuffle (pixel-shuffle / space-to-depth) to produce the high-resolution
//! output.
//!
//! # Architecture (2× upscale)
//!
//! 1. **Feature extraction** — 5×5 convolution, 64 feature maps, tanh.
//! 2. **Non-linear mapping**  — 3×3 convolution, 32 feature maps, tanh.
//! 3. **Sub-pixel output**    — 3×3 convolution, `channels × scale²` maps.
//! 4. **Pixel shuffle**       — rearranges feature maps into spatial pixels.
//!
//! Weights are initialised with a deterministic pseudo-random scheme
//! (LCG-based) that produces reasonable sharpening behaviour without requiring
//! external weight files.  For production use, replace `init_weights` with a
//! loader that reads weights from a file trained offline on super-resolution
//! datasets.
//!
//! # Example
//!
//! ```
//! use oximedia_scaling::neural_upscale::{NeuralUpscaler, UpscaleFactor};
//!
//! let upscaler = NeuralUpscaler::new(UpscaleFactor::X2);
//! let input = vec![128u8; 4 * 4 * 3]; // 4×4 RGB
//! let output = upscaler.upscale(&input, 4, 4, 3).expect("upscale ok");
//! assert_eq!(output.len(), 8 * 8 * 3);
//! ```

#![allow(clippy::cast_precision_loss)]
#![allow(clippy::cast_possible_truncation)]
#![allow(clippy::cast_sign_loss)]

use rayon::prelude::*;

use crate::ScalingError;

// ---------------------------------------------------------------------------
// Scale factor
// ---------------------------------------------------------------------------

/// Neural upscale factor.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UpscaleFactor {
    /// 2× upscaling (output is 2× wider and 2× taller).
    X2,
    /// 4× upscaling (output is 4× wider and 4× taller).
    X4,
}

impl UpscaleFactor {
    /// Integer scale multiplier.
    #[must_use]
    pub fn as_u32(self) -> u32 {
        match self {
            Self::X2 => 2,
            Self::X4 => 4,
        }
    }

    /// Number of sub-pixel output channels per input channel.
    ///
    /// Equal to `scale²`.
    #[must_use]
    pub fn sub_pixel_count(self) -> usize {
        let s = self.as_u32() as usize;
        s * s
    }
}

// ---------------------------------------------------------------------------
// Kernel helpers
// ---------------------------------------------------------------------------

/// A 2D convolution kernel.
struct Kernel {
    /// Row-major coefficients, shape `[out_ch][in_ch][kh][kw]`.
    weights: Vec<f32>,
    /// Bias per output channel.
    bias: Vec<f32>,
    out_channels: usize,
    in_channels: usize,
    kernel_size: usize,
}

impl Kernel {
    /// Initialise kernel weights with a deterministic LCG scheme.
    ///
    /// Uses He initialisation variance (`√(2 / fan_in)`).
    fn new_lcg(out_ch: usize, in_ch: usize, ksize: usize, seed: u64) -> Self {
        let fan_in = (in_ch * ksize * ksize) as f64;
        let std = (2.0 / fan_in).sqrt() as f32;

        let total = out_ch * in_ch * ksize * ksize;
        let mut weights = Vec::with_capacity(total);
        let mut state: u64 = seed;

        for _ in 0..total {
            // LCG: x_{n+1} = (a·x_n + c) mod 2^64
            state = state
                .wrapping_mul(6_364_136_223_846_793_005)
                .wrapping_add(1_442_695_040_888_963_407);
            // Map to [-1, 1]
            let norm = (state >> 33) as f32 / (u32::MAX as f32) * 2.0 - 1.0;
            weights.push(norm * std);
        }

        let bias = vec![0.0f32; out_ch];
        Self {
            weights,
            bias,
            out_channels: out_ch,
            in_channels: in_ch,
            kernel_size: ksize,
        }
    }

    /// Apply this kernel as a 2D convolution (SAME padding) to a feature map.
    ///
    /// Input `map` has shape `[in_ch][h][w]`.
    /// Output has shape `[out_ch][h][w]`.
    fn convolve(&self, map: &[f32], h: usize, w: usize) -> Vec<f32> {
        let kh = self.kernel_size;
        let kw = self.kernel_size;
        let pad = kh / 2;
        let total = self.out_channels * h * w;

        let mut out = vec![0.0f32; total];

        // Parallel over output channels
        out.par_chunks_mut(h * w)
            .enumerate()
            .for_each(|(oc, slice)| {
                let bias_val = self.bias[oc];
                for row in 0..h {
                    for col in 0..w {
                        let mut acc = bias_val;
                        for ic in 0..self.in_channels {
                            for kr in 0..kh {
                                let sr = row + kr;
                                if sr < pad || sr >= h + pad {
                                    continue;
                                }
                                let sr = sr - pad;
                                for kc in 0..kw {
                                    let sc = col + kc;
                                    if sc < pad || sc >= w + pad {
                                        continue;
                                    }
                                    let sc = sc - pad;
                                    let w_idx = oc * self.in_channels * kh * kw
                                        + ic * kh * kw
                                        + kr * kw
                                        + kc;
                                    let i_idx = ic * h * w + sr * w + sc;
                                    acc += self.weights[w_idx] * map[i_idx];
                                }
                            }
                        }
                        slice[row * w + col] = acc;
                    }
                }
            });

        out
    }
}

// ---------------------------------------------------------------------------
// Activation helpers
// ---------------------------------------------------------------------------

fn tanh_inplace(v: &mut [f32]) {
    for x in v.iter_mut() {
        *x = x.tanh();
    }
}

// ---------------------------------------------------------------------------
// Pixel shuffle
// ---------------------------------------------------------------------------

/// Rearrange `[C * r², H, W]` feature maps to `[C, H*r, W*r]`.
fn pixel_shuffle(input: &[f32], channels: usize, r: usize, h: usize, w: usize) -> Vec<f32> {
    let out_h = h * r;
    let out_w = w * r;
    let mut out = vec![0.0f32; channels * out_h * out_w];

    for c in 0..channels {
        for sh in 0..r {
            for sw in 0..r {
                let in_channel = c * r * r + sh * r + sw;
                for row in 0..h {
                    for col in 0..w {
                        let out_row = row * r + sh;
                        let out_col = col * r + sw;
                        let in_idx = in_channel * h * w + row * w + col;
                        let out_idx = c * out_h * out_w + out_row * out_w + out_col;
                        out[out_idx] = input[in_idx];
                    }
                }
            }
        }
    }

    out
}

// ---------------------------------------------------------------------------
// NeuralUpscaler
// ---------------------------------------------------------------------------

/// Lightweight ESPCN-style neural upscaler.
///
/// All inference is performed in the low-resolution domain; the sub-pixel
/// shuffle expands to the target resolution in a single efficient step.
pub struct NeuralUpscaler {
    factor: UpscaleFactor,
    /// Layer 1: feature extraction 5×5, 1→64 (applied per channel).
    layer1: Kernel,
    /// Layer 2: non-linear mapping 3×3, 64→32.
    layer2: Kernel,
    /// Layer 3: sub-pixel output 3×3, 32→(scale²).
    layer3: Kernel,
}

impl NeuralUpscaler {
    /// Create a new neural upscaler for the given scale factor.
    ///
    /// Weights are deterministically initialised from a fixed seed; replace
    /// `layer1/2/3` weight fields with trained weights for production use.
    #[must_use]
    pub fn new(factor: UpscaleFactor) -> Self {
        let sub = factor.sub_pixel_count();
        Self {
            factor,
            layer1: Kernel::new_lcg(64, 1, 5, 0xDEAD_BEEF_1234_5678),
            layer2: Kernel::new_lcg(32, 64, 3, 0xCAFE_BABE_8765_4321),
            layer3: Kernel::new_lcg(sub, 32, 3, 0xABCD_EF01_2345_6789),
        }
    }

    /// Upscale a packed image buffer.
    ///
    /// `channels` must be 1 (grayscale), 3 (RGB), or 4 (RGBA).
    /// Alpha channels are passed through with bilinear interpolation; only
    /// the colour/luma channels go through the neural network.
    ///
    /// # Errors
    ///
    /// Returns [`ScalingError`] if dimensions are zero or `channels` is
    /// unsupported.
    pub fn upscale(
        &self,
        input: &[u8],
        src_width: usize,
        src_height: usize,
        channels: usize,
    ) -> Result<Vec<u8>, ScalingError> {
        if src_width == 0 || src_height == 0 {
            return Err(ScalingError::InvalidDimensions(format!(
                "src dimensions must be non-zero, got {src_width}×{src_height}"
            )));
        }
        if channels == 0 || channels > 4 {
            return Err(ScalingError::InvalidDimensions(format!(
                "neural_upscale: unsupported channel count {channels}"
            )));
        }

        let expected = src_width * src_height * channels;
        if input.len() != expected {
            return Err(ScalingError::InsufficientBuffer {
                expected,
                actual: input.len(),
            });
        }

        let scale = self.factor.as_u32() as usize;
        let dst_width = src_width * scale;
        let dst_height = src_height * scale;
        let mut output = vec![0u8; dst_width * dst_height * channels];

        // Process each colour channel independently
        let colour_channels = channels.min(3);
        for ch in 0..colour_channels {
            // Extract single channel as float
            let mut plane: Vec<f32> = (0..src_height)
                .flat_map(|row| (0..src_width).map(move |col| row * src_width + col))
                .map(|idx| input[idx * channels + ch] as f32 / 255.0)
                .collect();

            // Layer 1: feature extraction (treat single plane as 1 input channel)
            let mut feat = self.layer1.convolve(&plane, src_height, src_width);
            tanh_inplace(&mut feat);

            // Layer 2: non-linear mapping
            let mut feat2 = self.layer2.convolve(&feat, src_height, src_width);
            tanh_inplace(&mut feat2);

            // Layer 3: sub-pixel output (scale² output channels)
            let feat3 = self.layer3.convolve(&feat2, src_height, src_width);

            // Pixel shuffle to produce HQ plane
            let shuffled = pixel_shuffle(&feat3, 1, scale, src_height, src_width);

            // Clamp and write to output
            for row in 0..dst_height {
                for col in 0..dst_width {
                    let v = shuffled[row * dst_width + col].clamp(0.0, 1.0);
                    output[(row * dst_width + col) * channels + ch] = (v * 255.0 + 0.5) as u8;
                }
            }

            // Suppress unused warning on last iteration
            let _ = &mut plane;
        }

        // Pass alpha through (bilinear nearest-neighbour for simplicity)
        if channels == 4 {
            for row in 0..dst_height {
                for col in 0..dst_width {
                    let src_row = (row / scale).min(src_height - 1);
                    let src_col = (col / scale).min(src_width - 1);
                    let src_idx = (src_row * src_width + src_col) * channels + 3;
                    let dst_idx = (row * dst_width + col) * channels + 3;
                    output[dst_idx] = input[src_idx];
                }
            }
        }

        Ok(output)
    }

    /// Return the upscale factor.
    #[must_use]
    pub fn factor(&self) -> UpscaleFactor {
        self.factor
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_upscale_factor_values() {
        assert_eq!(UpscaleFactor::X2.as_u32(), 2);
        assert_eq!(UpscaleFactor::X4.as_u32(), 4);
        assert_eq!(UpscaleFactor::X2.sub_pixel_count(), 4);
        assert_eq!(UpscaleFactor::X4.sub_pixel_count(), 16);
    }

    #[test]
    fn test_upscale_2x_output_size() {
        let upscaler = NeuralUpscaler::new(UpscaleFactor::X2);
        let input = vec![128u8; 4 * 4 * 3];
        let output = upscaler.upscale(&input, 4, 4, 3).expect("upscale ok");
        assert_eq!(output.len(), 8 * 8 * 3);
    }

    #[test]
    fn test_upscale_4x_output_size() {
        let upscaler = NeuralUpscaler::new(UpscaleFactor::X4);
        let input = vec![200u8; 2 * 2 * 1];
        let output = upscaler.upscale(&input, 2, 2, 1).expect("upscale ok");
        assert_eq!(output.len(), 8 * 8 * 1);
    }

    #[test]
    fn test_upscale_rgba_alpha_passthrough() {
        let upscaler = NeuralUpscaler::new(UpscaleFactor::X2);
        let mut input = vec![100u8; 2 * 2 * 4];
        // Set alpha to 200
        for i in 0..4 {
            input[i * 4 + 3] = 200;
        }
        let output = upscaler.upscale(&input, 2, 2, 4).expect("ok");
        // All alpha samples in the output should be 200 (nearest-neighbour passthrough)
        for i in 0..(4 * 4) {
            assert_eq!(output[i * 4 + 3], 200, "alpha mismatch at pixel {i}");
        }
    }

    #[test]
    fn test_upscale_zero_width_returns_err() {
        let upscaler = NeuralUpscaler::new(UpscaleFactor::X2);
        let result = upscaler.upscale(&[], 0, 4, 3);
        assert!(result.is_err());
    }

    #[test]
    fn test_upscale_bad_channel_count_returns_err() {
        let upscaler = NeuralUpscaler::new(UpscaleFactor::X2);
        let result = upscaler.upscale(&vec![0u8; 4 * 4 * 5], 4, 4, 5);
        assert!(result.is_err());
    }

    #[test]
    fn test_pixel_shuffle_correct_shape() {
        // r=2, H=2, W=2, C=1 → 4 input channels → 4×4 output
        let input: Vec<f32> = (0..16).map(|x| x as f32).collect();
        let out = pixel_shuffle(&input, 1, 2, 2, 2);
        assert_eq!(out.len(), 1 * 4 * 4);
    }
}