zoomvtools 2.0.0

Video motion vector analysis utilities in pure Rust
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
#[cfg(test)]
mod tests;

use std::{
    cmp::{max, min},
    num::NonZeroUsize,
};

use anyhow::{Result, anyhow, bail, ensure};
use safefma::Fma;
use semisafe::slice::get as semisafe_get;
use semisafe::slice::get_mut as semisafe_get_mut;
use semisafe::slice::split_at_mut as semisafe_split_at_mut;

use crate::{
    analysis::MVAnalysisData,
    fake::group_of_planes::FakeGroupOfPlanes,
    frame::{FramePlanesMut, FrameView, PlaneSizeTuple},
    mv::MotionVector,
    params::{MaskKind, Subpel},
    resize::SimpleResize,
    util::vs_bitblt,
    video::{ColorFamily, Resolution, SampleType, VideoInfo},
};

/// Options used to construct [`Mask`].
#[derive(Debug, Clone, Copy)]
pub struct MaskOptions {
    /// Motion-length normalization factor.
    pub ml: f32,
    /// Gamma curve applied to the generated mask.
    pub gamma: f32,
    /// Mask interpretation mode.
    pub kind: MaskKind,
    /// Position within the motion field in the `0.0..=100.0` range.
    pub time: f64,
    /// Luma value written when vectors are marked as scene changed.
    pub scene_change_value: i32,
}

impl Default for MaskOptions {
    #[inline]
    fn default() -> Self {
        Self {
            ml: 100.0,
            gamma: 1.0,
            kind: MaskKind::default(),
            time: 100.0,
            scene_change_value: 0,
        }
    }
}

/// Motion-derived mask generator.
pub struct Mask {
    info: VideoInfo,
    vectors_data: MVAnalysisData,
    thscd1: u64,
    thscd2: u64,
    gamma: f32,
    kind: MaskKind,
    time256: i32,
    scene_change_value: i32,
    mask_norm_factor: f32,
    mask_norm_factor2: f32,
    half_gamma: f32,
    width_b: NonZeroUsize,
    width_b_uv: NonZeroUsize,
    height_b: NonZeroUsize,
    height_b_uv: NonZeroUsize,
    upsizer: SimpleResize,
    upsizer_uv: SimpleResize,
}

impl Mask {
    /// Builds a mask generator from validated clip and vector metadata.
    ///
    /// # Errors
    /// Returns an error if the clip format, vector metadata, thresholds, or options are invalid.
    #[inline]
    pub fn new(
        info: VideoInfo,
        vectors_data: MVAnalysisData,
        options: MaskOptions,
        thscd1: Option<u64>,
        thscd2: Option<u64>,
    ) -> Result<Self> {
        ensure!(options.ml > 0.0, "Mask: ml must be greater than 0.");
        ensure!(options.gamma >= 0.0, "Mask: gamma must not be negative.");
        ensure!(
            (0.0..=100.0).contains(&options.time),
            "Mask: time must be between 0.0 and 100.0 (inclusive)."
        );
        ensure!(
            (0..=255).contains(&options.scene_change_value),
            "Mask: ysc must be between 0 and 255 (inclusive)."
        );

        if info.format.bits_per_sample.get() > 8
            || info.format.sample_type != SampleType::Integer
            || ![ColorFamily::Yuv, ColorFamily::Gray].contains(&info.format.color_family)
            || info.format.sub_sampling_w > 1
            || info.format.sub_sampling_h > 1
        {
            bail!(
                "Mask: input clip must be GRAY8, YUV420P8, YUV422P8, YUV440P8, or YUV444P8, with constant dimensions."
            );
        }
        ensure!(
            info.resolution.width == vectors_data.width.get()
                && info.resolution.height == vectors_data.height.get(),
            "Mask: wrong source or super clip frame size."
        );

        let mask_norm_factor = 1.0 / options.ml;
        let mask_norm_factor2 = mask_norm_factor.powi(2);
        let half_gamma = options.gamma * 0.5;
        let mut thscd1 = thscd1.unwrap_or(crate::params::MV_DEFAULT_SCD1);
        let mut thscd2 = thscd2.unwrap_or(crate::params::MV_DEFAULT_SCD2);
        vectors_data.scale_thscd(&mut thscd1, &mut thscd2, "Mask")?;
        let x_ratio_uv = vectors_data.x_ratio_uv.get() as usize;
        let y_ratio_uv = vectors_data.y_ratio_uv.get() as usize;
        let width_uv = NonZeroUsize::new(vectors_data.width.get() / x_ratio_uv)
            .ok_or_else(|| anyhow!("Mask: wrong source or super clip frame size."))?;
        let height_uv = NonZeroUsize::new(vectors_data.height.get() / y_ratio_uv)
            .ok_or_else(|| anyhow!("Mask: wrong source or super clip frame size."))?;
        let overlap_x = vectors_data.overlap_x;
        let overlap_y = vectors_data.overlap_y;
        let blk_size_x = vectors_data.blk_size_x;
        let blk_size_y = vectors_data.blk_size_y;
        let width_b = NonZeroUsize::new(
            vectors_data.blk_x.get() * (blk_size_x.get() - overlap_x) + overlap_x,
        )
        .ok_or_else(|| anyhow!("Mask: wrong source or super clip frame size."))?;
        let width_b_uv = NonZeroUsize::new(width_b.get() / x_ratio_uv)
            .ok_or_else(|| anyhow!("Mask: wrong source or super clip frame size."))?;
        let height_b = NonZeroUsize::new(
            vectors_data.blk_y.get() * (blk_size_y.get() - overlap_y) + overlap_y,
        )
        .ok_or_else(|| anyhow!("Mask: wrong source or super clip frame size."))?;
        let height_b_uv = NonZeroUsize::new(height_b.get() / y_ratio_uv)
            .ok_or_else(|| anyhow!("Mask: wrong source or super clip frame size."))?;

        Ok(Self {
            info,
            vectors_data,
            thscd1,
            thscd2,
            gamma: options.gamma,
            kind: options.kind,
            time256: (options.time * 256.0 / 100.0) as i32,
            scene_change_value: options.scene_change_value,
            mask_norm_factor,
            mask_norm_factor2,
            half_gamma,
            width_b,
            width_b_uv,
            height_b,
            height_b_uv,
            upsizer: SimpleResize::new(
                width_b,
                height_b,
                vectors_data.blk_x,
                vectors_data.blk_y,
                vectors_data.width,
                vectors_data.height,
                vectors_data.pel,
            ),
            upsizer_uv: SimpleResize::new(
                width_b_uv,
                height_b_uv,
                vectors_data.blk_x,
                vectors_data.blk_y,
                width_uv,
                height_uv,
                vectors_data.pel,
            ),
        })
    }

    /// Returns the output frame size for the generated mask.
    #[must_use]
    #[inline]
    pub const fn output_resolution(&self) -> Resolution {
        Resolution {
            width: self.vectors_data.width.get(),
            height: self.vectors_data.height.get(),
        }
    }

    /// Renders one 8-bit mask frame into `output`.
    ///
    /// # Errors
    /// Returns an error if the source frame, vectors, or output buffers do not match the filter
    /// configuration.
    #[inline]
    pub fn render_frame(
        &self,
        src: &FrameView<'_, u8>,
        output: &mut FramePlanesMut<'_, u8>,
        output_pitch: PlaneSizeTuple,
        vectors: &FakeGroupOfPlanes,
    ) -> Result<()> {
        let plane_count = self.info.format.plane_count();
        let luma_stride = pitch_for_plane(output_pitch, 0)?;
        let width = self.vectors_data.width;
        let height = self.vectors_data.height;
        let width_uv = NonZeroUsize::new(
            self.vectors_data.width.get() / self.vectors_data.x_ratio_uv.get() as usize,
        )
        .expect("validated during construction");
        let height_uv = NonZeroUsize::new(
            self.vectors_data.height.get() / self.vectors_data.y_ratio_uv.get() as usize,
        )
        .expect("validated during construction");

        if vectors.is_usable(self.thscd1, self.thscd2) {
            let blk_x = self.vectors_data.blk_x;
            let blk_y = self.vectors_data.blk_y;
            let blk_count = blk_x.get() * blk_y.get();
            let pel = self.vectors_data.pel;
            let blk_step_x = self.vectors_data.blk_size_x.get() - self.vectors_data.overlap_x;
            let blk_step_y = self.vectors_data.blk_size_y.get() - self.vectors_data.overlap_y;

            let (small_mask, small_mask_v) = match self.kind {
                MaskKind::VectorLength => {
                    let mut small_mask = Vec::with_capacity(blk_count);
                    for block_index in 0..blk_count {
                        let vector = vectors.get_block(0, block_index).vector;
                        small_mask.push(self.vector_length(vector, pel));
                    }
                    (small_mask, None)
                }
                MaskKind::SadMask => (
                    self.make_sad_mask(
                        vectors,
                        blk_x,
                        blk_y,
                        4.0 * self.mask_norm_factor as f64
                            / (self.vectors_data.blk_size_x.get()
                                * self.vectors_data.blk_size_y.get())
                                as f64,
                        self.gamma as f64,
                        pel,
                        blk_x,
                        self.time256,
                        blk_step_x,
                        blk_step_y,
                        8,
                    ),
                    None,
                ),
                MaskKind::OcclusionMask => (
                    self.make_vector_occlusion_mask(
                        vectors,
                        self.vectors_data.is_backward,
                        blk_x,
                        blk_y,
                        1.0 / self.mask_norm_factor as f64,
                        self.gamma as f64,
                        pel,
                        blk_x,
                        self.time256,
                        blk_step_x,
                        blk_step_y,
                    ),
                    None,
                ),
                MaskKind::HorizontalMotion => {
                    let mut small_mask = Vec::with_capacity(blk_count);
                    for block_index in 0..blk_count {
                        let vector = vectors.get_block(0, block_index).vector;
                        small_mask.push(horizontal_component(vector, self.mask_norm_factor));
                    }
                    (small_mask, None)
                }
                MaskKind::VerticalMotion => {
                    let mut small_mask = Vec::with_capacity(blk_count);
                    for block_index in 0..blk_count {
                        let vector = vectors.get_block(0, block_index).vector;
                        small_mask.push(vertical_component(vector, self.mask_norm_factor));
                    }
                    (small_mask, None)
                }
                MaskKind::MotionColormap => {
                    let mut small_mask = Vec::with_capacity(blk_count);
                    let mut small_mask_v = Vec::with_capacity(blk_count);
                    for block_index in 0..blk_count {
                        let vector = vectors.get_block(0, block_index).vector;
                        small_mask.push(horizontal_component(vector, self.mask_norm_factor));
                        small_mask_v.push(vertical_component(vector, self.mask_norm_factor));
                    }
                    (small_mask, Some(small_mask_v))
                }
            };

            if self.kind == MaskKind::MotionColormap {
                copy_plane_rows(
                    output.plane_mut(0)?,
                    luma_stride,
                    src.plane(0)?,
                    src.pitch_for_plane(0)?,
                    width,
                    height,
                );
            } else {
                let dest = output.plane_mut(0)?;
                self.upsizer
                    .resize_u8(dest, luma_stride, &small_mask, blk_x, false);
                extend_resized_plane(
                    dest,
                    luma_stride,
                    width,
                    self.width_b,
                    height,
                    self.height_b,
                );
            }

            if plane_count > 1 {
                let chroma_u_stride = pitch_for_plane(output_pitch, 1)?;
                let dest1 = output.plane_mut(1)?;
                self.upsizer_uv
                    .resize_u8(dest1, chroma_u_stride, &small_mask, blk_x, false);
                extend_resized_plane(
                    dest1,
                    chroma_u_stride,
                    width_uv,
                    self.width_b_uv,
                    height_uv,
                    self.height_b_uv,
                );

                let chroma_v_stride = pitch_for_plane(output_pitch, 2)?;
                let dest2 = output.plane_mut(2)?;
                let mask_v = small_mask_v.as_ref().unwrap_or(&small_mask);
                self.upsizer_uv
                    .resize_u8(dest2, chroma_v_stride, mask_v, blk_x, false);
                extend_resized_plane(
                    dest2,
                    chroma_v_stride,
                    width_uv,
                    self.width_b_uv,
                    height_uv,
                    self.height_b_uv,
                );
            }

            return Ok(());
        }

        if self.kind == MaskKind::MotionColormap {
            copy_plane_rows(
                output.plane_mut(0)?,
                luma_stride,
                src.plane(0)?,
                src.pitch_for_plane(0)?,
                width,
                height,
            );
        } else {
            fill_plane_prefix(
                output.plane_mut(0)?,
                luma_stride,
                height,
                self.scene_change_value as u8,
            );
        }

        if plane_count > 1 {
            fill_plane_prefix(
                output.plane_mut(1)?,
                pitch_for_plane(output_pitch, 1)?,
                height_uv,
                self.scene_change_value as u8,
            );
            fill_plane_prefix(
                output.plane_mut(2)?,
                pitch_for_plane(output_pitch, 2)?,
                height_uv,
                self.scene_change_value as u8,
            );
        }

        Ok(())
    }

    #[inline]
    fn vector_length(&self, vector: MotionVector, pel: Subpel) -> u8 {
        let norm_e = (vector.x.pow(2) + vector.y.pow(2)) as f64 / (pel as u32).pow(2) as f64;
        let length = 255.0 * (norm_e * self.mask_norm_factor2 as f64).powf(self.half_gamma as f64);
        if length > 255.0 { 255 } else { length as u8 }
    }

    #[inline]
    fn make_sad_mask(
        &self,
        fake_gop: &FakeGroupOfPlanes,
        blk_x: NonZeroUsize,
        blk_y: NonZeroUsize,
        sad_norm_factor: f64,
        gamma: f64,
        pel: Subpel,
        mask_pitch: NonZeroUsize,
        time256: i32,
        blk_step_x: usize,
        blk_step_y: usize,
        bits_per_sample: u8,
    ) -> Vec<u8> {
        let mut mask = vec![0; blk_y.get() * mask_pitch.get()];
        let pel = pel as i32;
        let time4096_x = (256 - time256) * 16 / (blk_step_x as i32 * pel);
        let time4096_y = (256 - time256) * 16 / (blk_step_y as i32 * pel);

        for by in 0..blk_y.get() {
            for bx in 0..blk_x.get() {
                let i = bx + by * blk_x.get();
                let block = fake_gop.get_block(0, i);
                let mut bxi = bx as i32 - block.vector.x * time4096_x / 4096;
                let mut byi = by as i32 - block.vector.y * time4096_y / 4096;
                if bxi < 0 || bxi >= blk_x.get() as i32 || byi < 0 || byi >= blk_y.get() as i32 {
                    bxi = bx as i32;
                    byi = by as i32;
                }

                let i1 = bxi as usize + byi as usize * blk_x.get();
                let sad = fake_gop.get_block(0, i1).vector.sad >> (bits_per_sample - 8);
                *semisafe_get_mut(&mut mask, bx + by * mask_pitch.get()) =
                    byte_norm(sad, sad_norm_factor, gamma);
            }
        }

        mask
    }

    #[inline]
    fn make_vector_occlusion_mask(
        &self,
        fake_gop: &FakeGroupOfPlanes,
        is_backward: bool,
        blk_x: NonZeroUsize,
        blk_y: NonZeroUsize,
        mask_norm_divider: f64,
        gamma: f64,
        pel: Subpel,
        mask_pitch: NonZeroUsize,
        time256: i32,
        blk_step_x: usize,
        blk_step_y: usize,
    ) -> Vec<u8> {
        let mut mask = vec![0; blk_y.get() * mask_pitch.get()];
        let pel = pel as i32;
        let time4096_x = time256 * 16 / (blk_step_x as i32 * pel);
        let time4096_y = time256 * 16 / (blk_step_y as i32 * pel);
        let occ_norm_x = 80.0 / (mask_norm_divider * blk_step_x as f64 * pel as f64);
        let occ_norm_y = 80.0 / (mask_norm_divider * blk_step_y as f64 * pel as f64);

        for by in 0..blk_y.get() {
            for bx in 0..blk_x.get() {
                let i = bx + by * blk_x.get();
                let block = fake_gop.get_block(0, i);
                let vx = block.vector.x;
                let vy = block.vector.y;

                if bx < blk_x.get() - 1 {
                    let block1 = fake_gop.get_block(0, i + 1);
                    if block1.vector.x < vx {
                        let occlusion = vx - block1.vector.x;
                        let min_b = if is_backward {
                            max(0, bx as i32 + 1 - occlusion * time4096_x / 4096) as usize
                        } else {
                            bx
                        };
                        let max_b = if is_backward {
                            bx + 1
                        } else {
                            min(
                                bx as i32 + 1 - occlusion * time4096_x / 4096,
                                blk_x.get() as i32 - 1,
                            ) as usize
                        };
                        for bxi in min_b..=max_b {
                            let mask_ptr = semisafe_get_mut(&mut mask, bxi + by * mask_pitch.get());
                            byte_occ_mask(mask_ptr, occlusion, occ_norm_x, gamma);
                        }
                    }
                }

                if by < blk_y.get() - 1 {
                    let block1 = fake_gop.get_block(0, i + blk_x.get());
                    if block1.vector.y < vy {
                        let occlusion = vy - block1.vector.y;
                        let min_b = if is_backward {
                            max(0, by as i32 + 1 - occlusion * time4096_y / 4096) as usize
                        } else {
                            by
                        };
                        let max_b = if is_backward {
                            by + 1
                        } else {
                            min(
                                by as i32 + 1 - occlusion * time4096_y / 4096,
                                blk_y.get() as i32 - 1,
                            ) as usize
                        };
                        for byi in min_b..=max_b {
                            let mask_ptr = semisafe_get_mut(&mut mask, bx + byi * mask_pitch.get());
                            byte_occ_mask(mask_ptr, occlusion, occ_norm_y, gamma);
                        }
                    }
                }
            }
        }

        mask
    }
}

#[inline]
fn pitch_for_plane(pitch: PlaneSizeTuple, plane: usize) -> Result<NonZeroUsize> {
    match plane {
        0 => Ok(pitch.0),
        1 => pitch
            .1
            .ok_or_else(|| anyhow!("requested plane {plane} is not available")),
        2 => pitch
            .2
            .ok_or_else(|| anyhow!("requested plane {plane} is not available")),
        _ => bail!("requested plane {plane} is not available"),
    }
}

#[inline]
fn horizontal_component(vector: MotionVector, mask_norm_factor: f32) -> u8 {
    ((vector.x as f32 * mask_norm_factor).fma(100.0, 128.0) as i32).clamp(0, 255) as u8
}

#[inline]
fn vertical_component(vector: MotionVector, mask_norm_factor: f32) -> u8 {
    ((vector.y as f32 * mask_norm_factor).fma(100.0, 128.0) as i32).clamp(0, 255) as u8
}

#[inline]
fn copy_plane_rows(
    dest: &mut [u8],
    dest_stride: NonZeroUsize,
    src: &[u8],
    src_stride: NonZeroUsize,
    width: NonZeroUsize,
    height: NonZeroUsize,
) {
    for row in 0..height.get() {
        let src_row = row * src_stride.get();
        let dst_row = row * dest_stride.get();
        let row_width = width.get();
        semisafe_get_mut(dest, dst_row..(dst_row + row_width))
            .copy_from_slice(semisafe_get(src, src_row..(src_row + row_width)));
        semisafe_get_mut(dest, (dst_row + row_width)..(dst_row + dest_stride.get())).fill(0);
    }
}

#[inline]
fn fill_plane_prefix(dest: &mut [u8], stride: NonZeroUsize, height: NonZeroUsize, value: u8) {
    let len = stride.get() * height.get();
    semisafe_get_mut(dest, ..len).fill(value);
}

#[inline]
fn extend_resized_plane(
    dest: &mut [u8],
    stride: NonZeroUsize,
    width: NonZeroUsize,
    width_b: NonZeroUsize,
    height: NonZeroUsize,
    height_b: NonZeroUsize,
) {
    if width > width_b {
        for h in 0..height.get() {
            for w in width_b.get()..width.get() {
                let val = *semisafe_get(dest, h * stride.get() + width_b.get() - 1);
                *semisafe_get_mut(dest, h * stride.get() + w) = val;
            }
        }
    }

    if height > height_b {
        let (before, target) = semisafe_split_at_mut(dest, height_b.get() * stride.get());
        let src = semisafe_get(before, ((height_b.get() - 1) * stride.get())..);
        vs_bitblt(
            target,
            stride,
            src,
            stride,
            width,
            NonZeroUsize::new(height.get() - height_b.get())
                .expect("height_b < height by condition"),
        );
    }
}

#[inline]
fn byte_occ_mask(mask_ptr: &mut u8, occlusion: i32, occ_norm: f64, gamma: f64) {
    *mask_ptr = max(
        *mask_ptr,
        if gamma == 1.0 {
            ((255.0 * occlusion as f64 * occ_norm) as i32).clamp(0, 255) as u8
        } else {
            ((255.0 * (occlusion as f64 * occ_norm).powf(gamma)) as i32).clamp(0, 255) as u8
        },
    );
}

#[inline]
fn byte_norm(sad: i64, sad_norm_factor: f64, gamma: f64) -> u8 {
    let length = 255.0 * (sad as f64 * sad_norm_factor).powf(gamma);
    if length > 255.0 { 255 } else { length as u8 }
}