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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
#![allow(clippy::unwrap_used, reason = "allow in test files")]
#![allow(clippy::undocumented_unsafe_blocks, reason = "allow in test files")]
#![allow(clippy::indexing_slicing, reason = "allow in test files")]

use std::num::NonZeroUsize;

use super::{pad_corner, pad_reference_frame};

/// Helper function to create a test frame with a specific pattern
/// Creates a frame with total size including padding, but only fills the inner
/// content area
fn create_test_frame<T: Copy + From<u8>>(
    width: usize,
    height: usize,
    hpad: usize,
    vpad: usize,
    pitch: usize,
) -> Vec<T> {
    let total_height = height + 2 * vpad;
    let mut frame = vec![T::from(0); pitch * total_height];

    // Fill the inner rectangle (actual content area) with a pattern for easy
    // verification
    for y in 0..height {
        for x in 0..width {
            let value = ((y * width + x) % 255) as u8;
            frame[(vpad + y) * pitch + hpad + x] = T::from(value);
        }
    }

    frame
}

/// Helper function to verify that padding was applied correctly
fn verify_padding<T: Copy + PartialEq + std::fmt::Debug>(
    frame: &[T],
    offset: usize,
    pitch: usize,
    hpad: usize,
    vpad: usize,
    width: usize,
    height: usize,
) {
    // Verify corners
    let top_left_value = frame[offset + vpad * pitch + hpad];
    let top_right_value = frame[offset + vpad * pitch + hpad + width - 1];
    let bottom_left_value = frame[offset + (vpad + height - 1) * pitch + hpad];
    let bottom_right_value = frame[offset + (vpad + height - 1) * pitch + hpad + width - 1];

    // Check top-left corner
    for y in 0..vpad {
        for x in 0..hpad {
            assert_eq!(
                frame[offset + y * pitch + x],
                top_left_value,
                "Top-left corner padding failed at ({}, {})",
                x,
                y
            );
        }
    }

    // Check top-right corner
    for y in 0..vpad {
        for x in 0..hpad {
            assert_eq!(
                frame[offset + y * pitch + hpad + width + x],
                top_right_value,
                "Top-right corner padding failed at ({}, {})",
                x,
                y
            );
        }
    }

    // Check bottom-left corner
    for y in 0..vpad {
        for x in 0..hpad {
            assert_eq!(
                frame[offset + (vpad + height + y) * pitch + x],
                bottom_left_value,
                "Bottom-left corner padding failed at ({}, {})",
                x,
                y
            );
        }
    }

    // Check bottom-right corner
    for y in 0..vpad {
        for x in 0..hpad {
            assert_eq!(
                frame[offset + (vpad + height + y) * pitch + hpad + width + x],
                bottom_right_value,
                "Bottom-right corner padding failed at ({}, {})",
                x,
                y
            );
        }
    }

    // Verify top edge
    for x in 0..width {
        let expected_value = frame[offset + vpad * pitch + hpad + x];
        for y in 0..vpad {
            assert_eq!(
                frame[offset + y * pitch + hpad + x],
                expected_value,
                "Top edge padding failed at ({}, {})",
                x,
                y
            );
        }
    }

    // Verify bottom edge
    for x in 0..width {
        let expected_value = frame[offset + (vpad + height - 1) * pitch + hpad + x];
        for y in 0..vpad {
            assert_eq!(
                frame[offset + (vpad + height + y) * pitch + hpad + x],
                expected_value,
                "Bottom edge padding failed at ({}, {})",
                x,
                y
            );
        }
    }

    // Verify left edge
    for y in 0..height {
        let expected_value = frame[offset + (vpad + y) * pitch + hpad];
        for x in 0..hpad {
            assert_eq!(
                frame[offset + (vpad + y) * pitch + x],
                expected_value,
                "Left edge padding failed at ({}, {})",
                x,
                y
            );
        }
    }

    // Verify right edge
    for y in 0..height {
        let expected_value = frame[offset + (vpad + y) * pitch + hpad + width - 1];
        for x in 0..hpad {
            assert_eq!(
                frame[offset + (vpad + y) * pitch + hpad + width + x],
                expected_value,
                "Right edge padding failed at ({}, {})",
                x,
                y
            );
        }
    }
}

#[test]
fn pad_reference_frame_u8_basic() {
    let width = 4;
    let height = 4;
    let hpad = 2;
    let vpad = 2;
    let pitch = width + 2 * hpad;

    let mut frame = create_test_frame::<u8>(width, height, hpad, vpad, pitch);
    let offset = 0;

    pad_reference_frame(
        offset,
        NonZeroUsize::new(pitch).unwrap(),
        hpad,
        vpad,
        NonZeroUsize::new(width).unwrap(),
        NonZeroUsize::new(height).unwrap(),
        &mut frame,
    );

    verify_padding(&frame, offset, pitch, hpad, vpad, width, height);
}

#[test]
fn pad_reference_frame_u16_basic() {
    let width = 4;
    let height = 4;
    let hpad = 2;
    let vpad = 2;
    let pitch = width + 2 * hpad;

    let mut frame = create_test_frame::<u16>(width, height, hpad, vpad, pitch);
    let offset = 0;

    pad_reference_frame(
        offset,
        NonZeroUsize::new(pitch).unwrap(),
        hpad,
        vpad,
        NonZeroUsize::new(width).unwrap(),
        NonZeroUsize::new(height).unwrap(),
        &mut frame,
    );

    verify_padding(&frame, offset, pitch, hpad, vpad, width, height);
}

#[test]
fn pad_reference_frame_with_offset() {
    let width = 3;
    let height = 3;
    let hpad = 1;
    let vpad = 1;
    let pitch = width + 2 * hpad;
    let offset = 10; // Non-zero offset

    let frame_size = offset + (height + 2 * vpad) * pitch;
    let mut frame = vec![0u8; frame_size];

    // Fill the actual frame area with test data
    for y in 0..height {
        for x in 0..width {
            let value = ((y * width + x + 1) * 10) as u8;
            frame[offset + (vpad + y) * pitch + hpad + x] = value;
        }
    }

    pad_reference_frame(
        offset,
        NonZeroUsize::new(pitch).unwrap(),
        hpad,
        vpad,
        NonZeroUsize::new(width).unwrap(),
        NonZeroUsize::new(height).unwrap(),
        &mut frame,
    );

    verify_padding(&frame, offset, pitch, hpad, vpad, width, height);
}

#[test]
fn pad_reference_frame_minimal_size() {
    let width = 1;
    let height = 1;
    let hpad = 1;
    let vpad = 1;
    let pitch = width + 2 * hpad;

    let mut frame = create_test_frame::<u8>(width, height, hpad, vpad, pitch);
    let offset = 0;

    // Set a specific value for the single pixel
    frame[vpad * pitch + hpad] = 42;

    pad_reference_frame(
        offset,
        NonZeroUsize::new(pitch).unwrap(),
        hpad,
        vpad,
        NonZeroUsize::new(width).unwrap(),
        NonZeroUsize::new(height).unwrap(),
        &mut frame,
    );

    // All padding should be the value of the single pixel
    for y in 0..(height + 2 * vpad) {
        for x in 0..(width + 2 * hpad) {
            if y == vpad && x == hpad {
                continue; // Skip the original pixel
            }
            assert_eq!(frame[y * pitch + x], 42, "Padding failed at ({}, {})", x, y);
        }
    }
}

#[test]
fn pad_reference_frame_asymmetric_padding() {
    let width = 3;
    let height = 2;
    let hpad = 3; // Different horizontal padding
    let vpad = 1; // Different vertical padding
    let pitch = width + 2 * hpad;

    let mut frame = create_test_frame::<u16>(width, height, hpad, vpad, pitch);
    let offset = 0;

    pad_reference_frame(
        offset,
        NonZeroUsize::new(pitch).unwrap(),
        hpad,
        vpad,
        NonZeroUsize::new(width).unwrap(),
        NonZeroUsize::new(height).unwrap(),
        &mut frame,
    );

    verify_padding(&frame, offset, pitch, hpad, vpad, width, height);
}

#[test]
fn pad_reference_frame_large_frame() {
    let width = 16;
    let height = 12;
    let hpad = 4;
    let vpad = 3;
    let pitch = width + 2 * hpad;

    let mut frame = create_test_frame::<u8>(width, height, hpad, vpad, pitch);
    let offset = 0;

    pad_reference_frame(
        offset,
        NonZeroUsize::new(pitch).unwrap(),
        hpad,
        vpad,
        NonZeroUsize::new(width).unwrap(),
        NonZeroUsize::new(height).unwrap(),
        &mut frame,
    );

    verify_padding(&frame, offset, pitch, hpad, vpad, width, height);
}

#[test]
fn pad_reference_frame_wide_frame() {
    let width = 20;
    let height = 2;
    let hpad = 2;
    let vpad = 2;
    let pitch = width + 2 * hpad;

    let mut frame = create_test_frame::<u8>(width, height, hpad, vpad, pitch);
    let offset = 0;

    pad_reference_frame(
        offset,
        NonZeroUsize::new(pitch).unwrap(),
        hpad,
        vpad,
        NonZeroUsize::new(width).unwrap(),
        NonZeroUsize::new(height).unwrap(),
        &mut frame,
    );

    verify_padding(&frame, offset, pitch, hpad, vpad, width, height);
}

#[test]
fn pad_reference_frame_tall_frame() {
    let width = 2;
    let height = 20;
    let hpad = 2;
    let vpad = 2;
    let pitch = width + 2 * hpad;

    let mut frame = create_test_frame::<u16>(width, height, hpad, vpad, pitch);
    let offset = 0;

    pad_reference_frame(
        offset,
        NonZeroUsize::new(pitch).unwrap(),
        hpad,
        vpad,
        NonZeroUsize::new(width).unwrap(),
        NonZeroUsize::new(height).unwrap(),
        &mut frame,
    );

    verify_padding(&frame, offset, pitch, hpad, vpad, width, height);
}

#[test]
fn pad_reference_frame_pitch_larger_than_padded_width() {
    let width = 4;
    let height = 3;
    let hpad = 2;
    let vpad = 2;
    let pitch = width + 2 * hpad + 4; // Extra pitch

    let mut frame = create_test_frame::<u8>(width, height, hpad, vpad, pitch);
    let offset = 0;

    pad_reference_frame(
        offset,
        NonZeroUsize::new(pitch).unwrap(),
        hpad,
        vpad,
        NonZeroUsize::new(width).unwrap(),
        NonZeroUsize::new(height).unwrap(),
        &mut frame,
    );

    verify_padding(&frame, offset, pitch, hpad, vpad, width, height);
}

#[test]
fn pad_corner_basic() {
    let hpad = 2;
    let vpad = 2;
    let pitch = 6;
    let mut dest = vec![0u8; pitch * vpad];
    let val = 42u8;
    let offset = 0;

    // SAFETY: test function
    unsafe {
        pad_corner(
            offset,
            val,
            hpad,
            vpad,
            NonZeroUsize::new(pitch).unwrap(),
            &mut dest,
        );
    }

    // Verify all pixels in the corner are set to the expected value
    for y in 0..vpad {
        for x in 0..hpad {
            assert_eq!(
                dest[y * pitch + x],
                val,
                "Corner padding failed at ({}, {})",
                x,
                y
            );
        }
    }
}

#[test]
fn pad_corner_u16() {
    let hpad = 3;
    let vpad = 1;
    let pitch = 8;
    let mut dest = vec![0u16; pitch * vpad];
    let val = 1337u16;
    let offset = 0;

    // SAFETY: test function
    unsafe {
        pad_corner(
            offset,
            val,
            hpad,
            vpad,
            NonZeroUsize::new(pitch).unwrap(),
            &mut dest,
        );
    }

    // Verify all pixels in the corner are set to the expected value
    for y in 0..vpad {
        for x in 0..hpad {
            assert_eq!(
                dest[y * pitch + x],
                val,
                "Corner padding failed at ({}, {})",
                x,
                y
            );
        }
    }
}

#[test]
fn pad_corner_with_offset() {
    let hpad = 2;
    let vpad = 2;
    let pitch = 6;
    let offset = 12;
    let total_size = offset + pitch * vpad;
    let mut dest = vec![0u8; total_size];
    let val = 99u8;

    // SAFETY: test function
    unsafe {
        pad_corner(
            offset,
            val,
            hpad,
            vpad,
            NonZeroUsize::new(pitch).unwrap(),
            &mut dest,
        );
    }

    // Verify the area before offset is unchanged
    for (i, &value) in dest.iter().take(offset).enumerate() {
        assert_eq!(value, 0, "Data before offset was modified at index {}", i);
    }

    // Verify corner padding
    for y in 0..vpad {
        for x in 0..hpad {
            assert_eq!(
                dest[offset + y * pitch + x],
                val,
                "Corner padding failed at ({}, {})",
                x,
                y
            );
        }
    }
}

#[test]
fn pad_reference_frame_preserves_original_data() {
    let width = 3;
    let height = 3;
    let hpad = 1;
    let vpad = 1;
    let pitch = width + 2 * hpad;

    let mut frame = create_test_frame::<u8>(width, height, hpad, vpad, pitch);
    let original_data: Vec<u8> = frame
        .iter()
        .enumerate()
        .skip(vpad * pitch + hpad)
        .take(height * pitch - hpad)
        .filter(|(i, _)| {
            let row = (*i - vpad * pitch) / pitch;
            let col = (*i - vpad * pitch) % pitch;
            col >= hpad && col < hpad + width && row < height
        })
        .map(|(_, &val)| val)
        .collect();

    let offset = 0;

    pad_reference_frame(
        offset,
        NonZeroUsize::new(pitch).unwrap(),
        hpad,
        vpad,
        NonZeroUsize::new(width).unwrap(),
        NonZeroUsize::new(height).unwrap(),
        &mut frame,
    );

    // Verify original data is preserved
    let mut original_iter = original_data.iter();
    for y in 0..height {
        for x in 0..width {
            let actual = frame[offset + (vpad + y) * pitch + hpad + x];
            let expected = *original_iter.next().unwrap();
            assert_eq!(
                actual, expected,
                "Original data was modified at ({}, {})",
                x, y
            );
        }
    }
}

#[test]
fn pad_reference_frame_edge_values_correctness() {
    let width = 3;
    let height = 3;
    let hpad = 2;
    let vpad = 1;
    let pitch = width + 2 * hpad;

    let mut frame = vec![0u8; (height + 2 * vpad) * pitch];

    // Set specific values in the original frame area
    let values = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];

    for y in 0..height {
        for x in 0..width {
            frame[(vpad + y) * pitch + hpad + x] = values[y][x];
        }
    }

    let offset = 0;

    pad_reference_frame(
        offset,
        NonZeroUsize::new(pitch).unwrap(),
        hpad,
        vpad,
        NonZeroUsize::new(width).unwrap(),
        NonZeroUsize::new(height).unwrap(),
        &mut frame,
    );

    // Check specific edge values
    // Top edge should replicate first row
    for x in 0..width {
        for y in 0..vpad {
            assert_eq!(
                frame[y * pitch + hpad + x],
                values[0][x],
                "Top edge incorrect at ({}, {})",
                x,
                y
            );
        }
    }

    // Bottom edge should replicate last row
    for x in 0..width {
        for y in 0..vpad {
            assert_eq!(
                frame[(vpad + height + y) * pitch + hpad + x],
                values[height - 1][x],
                "Bottom edge incorrect at ({}, {})",
                x,
                y
            );
        }
    }

    // Left edge should replicate first column
    for y in 0..height {
        for x in 0..hpad {
            assert_eq!(
                frame[(vpad + y) * pitch + x],
                values[y][0],
                "Left edge incorrect at ({}, {})",
                x,
                y
            );
        }
    }

    // Right edge should replicate last column
    for y in 0..height {
        for x in 0..hpad {
            assert_eq!(
                frame[(vpad + y) * pitch + hpad + width + x],
                values[y][width - 1],
                "Right edge incorrect at ({}, {})",
                x,
                y
            );
        }
    }
}