zoomvtools 1.1.0

Video motion vector analysis utilities in pure Rust
Documentation
#![allow(clippy::unwrap_used, reason = "allow in test files")]
#![allow(clippy::indexing_slicing, reason = "allow in test files")]

use crate::{
    analysis::MVAnalysisData,
    fake::group_of_planes::FakeGroupOfPlanes,
    mv::MotionVector,
    params::{MotionFlags, Subpel},
};

fn fake_group_of_planes(blk_x: usize, blk_y: usize, vectors: &[(i32, i32)]) -> FakeGroupOfPlanes {
    use std::num::{NonZeroU8, NonZeroUsize};

    let analysis_data = MVAnalysisData {
        blk_size_x: NonZeroUsize::new(8).unwrap(),
        blk_size_y: NonZeroUsize::new(8).unwrap(),
        pel: Subpel::Full,
        level_count: 1,
        delta_frame: 1,
        is_backward: false,
        motion_flags: MotionFlags::empty(),
        width: NonZeroUsize::new(blk_x * 8).unwrap(),
        height: NonZeroUsize::new(blk_y * 8).unwrap(),
        overlap_x: 0,
        overlap_y: 0,
        blk_x: NonZeroUsize::new(blk_x).unwrap(),
        blk_y: NonZeroUsize::new(blk_y).unwrap(),
        bits_per_sample: NonZeroU8::new(8).unwrap(),
        y_ratio_uv: NonZeroU8::new(1).unwrap(),
        x_ratio_uv: NonZeroU8::new(1).unwrap(),
        h_padding: 0,
        v_padding: 0,
    };

    let mut fake_gop = FakeGroupOfPlanes::new(&analysis_data);
    for (block, (x, y)) in &mut fake_gop.planes[0]
        .blocks
        .iter_mut()
        .zip(vectors.iter().copied())
    {
        block.vector = MotionVector { x, y, sad: 0 };
    }
    fake_gop.validity = true;
    fake_gop
}

#[test]
fn check_and_pad_mask_small_repeats_last_row_and_column() {
    let mut mask = vec![1u8, 2, 0, 3, 4, 0, 0, 0, 0];

    super::check_and_pad_mask_small(&mut mask, 3, 3, 2, 2);

    assert_eq!(mask, vec![1, 2, 2, 3, 4, 4, 3, 4, 4]);
}

#[test]
fn make_vector_occlusion_mask_time_marks_occluded_gap() {
    let fake_gop = fake_group_of_planes(2, 1, &[(4, 0), (0, 0)]);

    let mask = super::make_vector_occlusion_mask_time(
        &fake_gop,
        false,
        2,
        1,
        100.0,
        1.0,
        Subpel::Full,
        2,
        1,
        128,
        8,
        8,
    );

    assert_eq!(mask, vec![102, 102]);
}

#[test]
fn make_vector_occlusion_mask_time_supports_padded_mask_height() {
    let fake_gop = fake_group_of_planes(1, 1, &[(0, 0)]);

    let mut mask = super::make_vector_occlusion_mask_time(
        &fake_gop,
        true,
        1,
        1,
        100.0,
        1.0,
        Subpel::Full,
        3,
        2,
        128,
        8,
        8,
    );

    super::check_and_pad_mask_small(&mut mask, 3, 2, 1, 1);

    assert_eq!(mask, vec![0; 6]);
}

#[test]
fn make_sad_mask_time_supports_padded_mask_height() {
    let fake_gop = fake_group_of_planes(1, 1, &[(0, 0)]);

    let mut mask =
        super::make_sad_mask_time(&fake_gop, 1, 1, 1.0, 1.0, Subpel::Full, 3, 2, 128, 8, 8, 8);

    super::check_and_pad_mask_small(&mut mask, 3, 2, 1, 1);

    assert_eq!(mask, vec![0; 6]);
}