av-scenechange 0.14.0

Estimates frames in a video where a scenecut would be ideal
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
#[cfg(test)]
mod tests;

#[cfg(not(any(asm_x86_64, asm_neon)))]
use rust::*;
#[cfg(asm_neon)]
use simd_neon::*;
#[cfg(asm_x86_64)]
use simd_x86::*;
use v_frame::pixel::Pixel;

use super::{block::BlockSize, plane::PlaneRegion};
use crate::cpu::CpuFeatureLevel;

mod rust {
    use v_frame::{
        math::msb,
        pixel::{CastFromPrimitive, Pixel},
    };

    use crate::{
        cpu::CpuFeatureLevel,
        data::{
            hadamard::{hadamard4x4, hadamard8x8},
            plane::{Area, PlaneRegion, Rect},
            sad::get_sad,
        },
    };

    /// Sum of absolute transformed differences over a block.
    /// w and h can be at most 128, the size of the largest block.
    /// Use the sum of 4x4 and 8x8 hadamard transforms for the transform, but
    /// revert to sad on edges when these transforms do not fit into w and h.
    /// 4x4 transforms instead of 8x8 transforms when width or height < 8.
    #[cfg_attr(all(asm_x86_64, target_feature = "avx2"), cold)]
    pub(super) fn get_satd_internal<T: Pixel>(
        plane_org: &PlaneRegion<'_, T>,
        plane_ref: &PlaneRegion<'_, T>,
        w: usize,
        h: usize,
        bit_depth: usize,
        cpu: CpuFeatureLevel,
    ) -> u32 {
        assert!(w <= 128 && h <= 128);
        assert!(plane_org.rect().width >= w && plane_org.rect().height >= h);
        assert!(plane_ref.rect().width >= w && plane_ref.rect().height >= h);

        // Size of hadamard transform should be 4x4 or 8x8
        // 4x* and *x4 use 4x4 and all other use 8x8
        let size: usize = w.min(h).min(8);
        let tx2d = if size == 4 { hadamard4x4 } else { hadamard8x8 };

        let mut sum: u64 = 0;

        // Loop over chunks the size of the chosen transform
        for chunk_y in (0..h).step_by(size) {
            let chunk_h = (h - chunk_y).min(size);
            for chunk_x in (0..w).step_by(size) {
                let chunk_w = (w - chunk_x).min(size);
                let chunk_area = Area::Rect(Rect {
                    x: chunk_x as isize,
                    y: chunk_y as isize,
                    width: chunk_w,
                    height: chunk_h,
                });
                let chunk_org = plane_org.subregion(chunk_area);
                let chunk_ref = plane_ref.subregion(chunk_area);

                // Revert to sad on edge blocks (frame edges)
                if chunk_w != size || chunk_h != size {
                    sum += get_sad(&chunk_org, &chunk_ref, chunk_w, chunk_h, bit_depth, cpu) as u64;
                    continue;
                }

                let buf: &mut [i32] = &mut [0; 8 * 8][..size * size];

                // Move the difference of the transforms to a buffer
                for (row_diff, (row_org, row_ref)) in buf
                    .chunks_mut(size)
                    .zip(chunk_org.rows_iter().zip(chunk_ref.rows_iter()))
                {
                    for (diff, (a, b)) in
                        row_diff.iter_mut().zip(row_org.iter().zip(row_ref.iter()))
                    {
                        *diff = i32::cast_from(*a) - i32::cast_from(*b);
                    }
                }

                // Perform the hadamard transform on the differences
                // SAFETY: A sufficient number elements exist for the size of the transform.
                unsafe {
                    tx2d(buf);
                }

                // Sum the absolute values of the transformed differences
                sum += buf.iter().map(|a| a.unsigned_abs() as u64).sum::<u64>();
            }
        }

        // Normalize the results
        let ln = msb(size as i32) as u64;
        ((sum + (1 << ln >> 1)) >> ln) as u32
    }
}

#[cfg(asm_x86_64)]
mod simd_x86 {
    use v_frame::pixel::{Pixel, PixelType};

    use super::{to_index, DIST_FNS_LENGTH};
    use crate::{
        cpu::CpuFeatureLevel,
        data::{block::BlockSize, plane::PlaneRegion},
    };

    type SatdFn = unsafe extern "C" fn(
        src: *const u8,
        src_stride: isize,
        dst: *const u8,
        dst_stride: isize,
    ) -> u32;

    type SatdHBDFn = unsafe extern "C" fn(
        src: *const u16,
        src_stride: isize,
        dst: *const u16,
        dst_stride: isize,
        bdmax: u32,
    ) -> u32;

    macro_rules! declare_asm_dist_fn {
        ($(($name: ident, $T: ident)),+) => (
            $(
            extern "C" { fn $name (
                src: *const $T, src_stride: isize, dst: *const $T, dst_stride: isize
            ) -> u32; }
            )+
        )
    }

    macro_rules! declare_asm_satd_hbd_fn {
        ($($name: ident),+) => (
            $(
            extern "C" { pub(crate) fn $name (
                src: *const u16, src_stride: isize, dst: *const u16, dst_stride: isize, bdmax: u32
            ) -> u32; }
            )+
        )
    }

    declare_asm_dist_fn![
        // SSSE3
        (avsc_satd_8x8_ssse3, u8),
        // SSE4
        (avsc_satd_4x4_sse4, u8),
        // AVX
        (avsc_satd_4x4_avx2, u8),
        (avsc_satd_8x8_avx2, u8),
        (avsc_satd_16x16_avx2, u8),
        (avsc_satd_32x32_avx2, u8),
        (avsc_satd_64x64_avx2, u8),
        (avsc_satd_128x128_avx2, u8),
        (avsc_satd_4x8_avx2, u8),
        (avsc_satd_8x4_avx2, u8),
        (avsc_satd_8x16_avx2, u8),
        (avsc_satd_16x8_avx2, u8),
        (avsc_satd_16x32_avx2, u8),
        (avsc_satd_32x16_avx2, u8),
        (avsc_satd_32x64_avx2, u8),
        (avsc_satd_64x32_avx2, u8),
        (avsc_satd_64x128_avx2, u8),
        (avsc_satd_128x64_avx2, u8),
        (avsc_satd_4x16_avx2, u8),
        (avsc_satd_16x4_avx2, u8),
        (avsc_satd_8x32_avx2, u8),
        (avsc_satd_32x8_avx2, u8),
        (avsc_satd_16x64_avx2, u8),
        (avsc_satd_64x16_avx2, u8)
    ];

    declare_asm_satd_hbd_fn![
        avsc_satd_4x4_hbd_avx2,
        avsc_satd_8x4_hbd_avx2,
        avsc_satd_4x8_hbd_avx2,
        avsc_satd_8x8_hbd_avx2,
        avsc_satd_16x8_hbd_avx2,
        avsc_satd_16x16_hbd_avx2,
        avsc_satd_32x32_hbd_avx2,
        avsc_satd_64x64_hbd_avx2,
        avsc_satd_128x128_hbd_avx2,
        avsc_satd_16x32_hbd_avx2,
        avsc_satd_16x64_hbd_avx2,
        avsc_satd_32x16_hbd_avx2,
        avsc_satd_32x64_hbd_avx2,
        avsc_satd_64x16_hbd_avx2,
        avsc_satd_64x32_hbd_avx2,
        avsc_satd_64x128_hbd_avx2,
        avsc_satd_128x64_hbd_avx2,
        avsc_satd_32x8_hbd_avx2,
        avsc_satd_8x16_hbd_avx2,
        avsc_satd_8x32_hbd_avx2,
        avsc_satd_16x4_hbd_avx2,
        avsc_satd_4x16_hbd_avx2
    ];

    static SATD_FNS_SSSE3: [Option<SatdFn>; DIST_FNS_LENGTH] = {
        let mut out: [Option<SatdFn>; DIST_FNS_LENGTH] = [None; DIST_FNS_LENGTH];

        use BlockSize::*;

        out[BLOCK_8X8 as usize] = Some(avsc_satd_8x8_ssse3);

        out
    };

    static SATD_FNS_SSE4_1: [Option<SatdFn>; DIST_FNS_LENGTH] = {
        let mut out: [Option<SatdFn>; DIST_FNS_LENGTH] = [None; DIST_FNS_LENGTH];

        use BlockSize::*;

        out[BLOCK_4X4 as usize] = Some(avsc_satd_4x4_sse4);
        out[BLOCK_8X8 as usize] = Some(avsc_satd_8x8_ssse3);

        out
    };

    static SATD_FNS_AVX2: [Option<SatdFn>; DIST_FNS_LENGTH] = {
        let mut out: [Option<SatdFn>; DIST_FNS_LENGTH] = [None; DIST_FNS_LENGTH];

        use BlockSize::*;

        out[BLOCK_4X4 as usize] = Some(avsc_satd_4x4_avx2);
        out[BLOCK_8X8 as usize] = Some(avsc_satd_8x8_avx2);
        out[BLOCK_16X16 as usize] = Some(avsc_satd_16x16_avx2);
        out[BLOCK_32X32 as usize] = Some(avsc_satd_32x32_avx2);
        out[BLOCK_64X64 as usize] = Some(avsc_satd_64x64_avx2);
        out[BLOCK_128X128 as usize] = Some(avsc_satd_128x128_avx2);

        out[BLOCK_4X8 as usize] = Some(avsc_satd_4x8_avx2);
        out[BLOCK_8X4 as usize] = Some(avsc_satd_8x4_avx2);
        out[BLOCK_8X16 as usize] = Some(avsc_satd_8x16_avx2);
        out[BLOCK_16X8 as usize] = Some(avsc_satd_16x8_avx2);
        out[BLOCK_16X32 as usize] = Some(avsc_satd_16x32_avx2);
        out[BLOCK_32X16 as usize] = Some(avsc_satd_32x16_avx2);
        out[BLOCK_32X64 as usize] = Some(avsc_satd_32x64_avx2);
        out[BLOCK_64X32 as usize] = Some(avsc_satd_64x32_avx2);
        out[BLOCK_64X128 as usize] = Some(avsc_satd_64x128_avx2);
        out[BLOCK_128X64 as usize] = Some(avsc_satd_128x64_avx2);

        out[BLOCK_4X16 as usize] = Some(avsc_satd_4x16_avx2);
        out[BLOCK_16X4 as usize] = Some(avsc_satd_16x4_avx2);
        out[BLOCK_8X32 as usize] = Some(avsc_satd_8x32_avx2);
        out[BLOCK_32X8 as usize] = Some(avsc_satd_32x8_avx2);
        out[BLOCK_16X64 as usize] = Some(avsc_satd_16x64_avx2);
        out[BLOCK_64X16 as usize] = Some(avsc_satd_64x16_avx2);

        out
    };

    cpu_function_lookup_table!(
      SATD_FNS: [[Option<SatdFn>; DIST_FNS_LENGTH]],
      default: [None; DIST_FNS_LENGTH],
      [SSSE3, SSE4_1, AVX2]
    );

    static SATD_HBD_FNS_AVX2: [Option<SatdHBDFn>; DIST_FNS_LENGTH] = {
        let mut out: [Option<SatdHBDFn>; DIST_FNS_LENGTH] = [None; DIST_FNS_LENGTH];

        use BlockSize::*;

        out[BLOCK_4X4 as usize] = Some(avsc_satd_4x4_hbd_avx2);
        out[BLOCK_8X8 as usize] = Some(avsc_satd_8x8_hbd_avx2);
        out[BLOCK_16X16 as usize] = Some(avsc_satd_16x16_hbd_avx2);
        out[BLOCK_32X32 as usize] = Some(avsc_satd_32x32_hbd_avx2);
        out[BLOCK_64X64 as usize] = Some(avsc_satd_64x64_hbd_avx2);
        out[BLOCK_128X128 as usize] = Some(avsc_satd_128x128_hbd_avx2);

        out[BLOCK_4X8 as usize] = Some(avsc_satd_4x8_hbd_avx2);
        out[BLOCK_8X4 as usize] = Some(avsc_satd_8x4_hbd_avx2);
        out[BLOCK_8X16 as usize] = Some(avsc_satd_8x16_hbd_avx2);
        out[BLOCK_16X8 as usize] = Some(avsc_satd_16x8_hbd_avx2);
        out[BLOCK_16X32 as usize] = Some(avsc_satd_16x32_hbd_avx2);
        out[BLOCK_32X16 as usize] = Some(avsc_satd_32x16_hbd_avx2);
        out[BLOCK_32X64 as usize] = Some(avsc_satd_32x64_hbd_avx2);
        out[BLOCK_64X32 as usize] = Some(avsc_satd_64x32_hbd_avx2);
        out[BLOCK_64X128 as usize] = Some(avsc_satd_64x128_hbd_avx2);
        out[BLOCK_128X64 as usize] = Some(avsc_satd_128x64_hbd_avx2);

        out[BLOCK_4X16 as usize] = Some(avsc_satd_4x16_hbd_avx2);
        out[BLOCK_16X4 as usize] = Some(avsc_satd_16x4_hbd_avx2);
        out[BLOCK_8X32 as usize] = Some(avsc_satd_8x32_hbd_avx2);
        out[BLOCK_32X8 as usize] = Some(avsc_satd_32x8_hbd_avx2);
        out[BLOCK_16X64 as usize] = Some(avsc_satd_16x64_hbd_avx2);
        out[BLOCK_64X16 as usize] = Some(avsc_satd_64x16_hbd_avx2);

        out
    };

    cpu_function_lookup_table!(
      SATD_HBD_FNS: [[Option<SatdHBDFn>; DIST_FNS_LENGTH]],
      default: [None; DIST_FNS_LENGTH],
      [AVX2]
    );

    pub(super) fn get_satd_internal<T: Pixel>(
        src: &PlaneRegion<'_, T>,
        dst: &PlaneRegion<'_, T>,
        w: usize,
        h: usize,
        bit_depth: usize,
        cpu: CpuFeatureLevel,
    ) -> u32 {
        let bsize_opt = BlockSize::from_width_and_height_opt(w, h);

        let call_rust =
            || -> u32 { super::rust::get_satd_internal(dst, src, w, h, bit_depth, cpu) };

        match (bsize_opt, T::type_enum()) {
            (Err(_), _) => call_rust(),
            (Ok(bsize), PixelType::U8) => {
                match SATD_FNS[cpu.as_index()][to_index(bsize)] {
                    // SAFETY: Calls Assembly code.
                    Some(func) => unsafe {
                        func(
                            src.data_ptr() as *const _,
                            T::to_asm_stride(src.plane_cfg.stride),
                            dst.data_ptr() as *const _,
                            T::to_asm_stride(dst.plane_cfg.stride),
                        )
                    },
                    None => call_rust(),
                }
            }
            (Ok(bsize), PixelType::U16) => {
                match SATD_HBD_FNS[cpu.as_index()][to_index(bsize)] {
                    // SAFETY: Calls Assembly code.
                    Some(func) => unsafe {
                        func(
                            src.data_ptr() as *const _,
                            T::to_asm_stride(src.plane_cfg.stride),
                            dst.data_ptr() as *const _,
                            T::to_asm_stride(dst.plane_cfg.stride),
                            (1 << bit_depth) - 1,
                        )
                    },
                    None => call_rust(),
                }
            }
        }
    }
}

#[cfg(asm_neon)]
mod simd_neon {
    use v_frame::pixel::{Pixel, PixelType};

    use super::{to_index, DIST_FNS_LENGTH};
    use crate::{
        cpu::CpuFeatureLevel,
        data::{block::BlockSize, plane::PlaneRegion},
    };

    type SatdFn = unsafe extern "C" fn(
        src: *const u8,
        src_stride: isize,
        dst: *const u8,
        dst_stride: isize,
    ) -> u32;
    type SatdHbdFn = unsafe extern "C" fn(
        src: *const u16,
        src_stride: isize,
        dst: *const u16,
        dst_stride: isize,
    ) -> u32;

    macro_rules! declare_asm_dist_fn {
        ($(($name: ident, $T: ident)),+) => (
            $(
                extern "C" { fn $name (
                    src: *const $T, src_stride: isize, dst: *const $T, dst_stride: isize
                ) -> u32; }
            )+
        )
    }

    declare_asm_dist_fn![
        // SATD
        (avsc_satd4x4_neon, u8),
        (avsc_satd4x8_neon, u8),
        (avsc_satd4x16_neon, u8),
        (avsc_satd8x4_neon, u8),
        (avsc_satd8x8_neon, u8),
        (avsc_satd8x16_neon, u8),
        (avsc_satd8x32_neon, u8),
        (avsc_satd16x4_neon, u8),
        (avsc_satd16x8_neon, u8),
        (avsc_satd16x16_neon, u8),
        (avsc_satd16x32_neon, u8),
        (avsc_satd16x64_neon, u8),
        (avsc_satd32x8_neon, u8),
        (avsc_satd32x16_neon, u8),
        (avsc_satd32x32_neon, u8),
        (avsc_satd32x64_neon, u8),
        (avsc_satd64x16_neon, u8),
        (avsc_satd64x32_neon, u8),
        (avsc_satd64x64_neon, u8),
        (avsc_satd64x128_neon, u8),
        (avsc_satd128x64_neon, u8),
        (avsc_satd128x128_neon, u8),
        // SATD HBD
        (avsc_satd4x4_hbd_neon, u16),
        (avsc_satd4x8_hbd_neon, u16),
        (avsc_satd4x16_hbd_neon, u16),
        (avsc_satd8x4_hbd_neon, u16),
        (avsc_satd8x8_hbd_neon, u16),
        (avsc_satd8x16_hbd_neon, u16),
        (avsc_satd8x32_hbd_neon, u16),
        (avsc_satd16x4_hbd_neon, u16),
        (avsc_satd16x8_hbd_neon, u16),
        (avsc_satd16x16_hbd_neon, u16),
        (avsc_satd16x32_hbd_neon, u16),
        (avsc_satd16x64_hbd_neon, u16),
        (avsc_satd32x8_hbd_neon, u16),
        (avsc_satd32x16_hbd_neon, u16),
        (avsc_satd32x32_hbd_neon, u16),
        (avsc_satd32x64_hbd_neon, u16),
        (avsc_satd64x16_hbd_neon, u16),
        (avsc_satd64x32_hbd_neon, u16),
        (avsc_satd64x64_hbd_neon, u16),
        (avsc_satd64x128_hbd_neon, u16),
        (avsc_satd128x64_hbd_neon, u16),
        (avsc_satd128x128_hbd_neon, u16)
    ];

    static SATD_FNS_NEON: [Option<SatdFn>; DIST_FNS_LENGTH] = {
        let mut out: [Option<SatdFn>; DIST_FNS_LENGTH] = [None; DIST_FNS_LENGTH];

        use crate::data::block::BlockSize::*;

        out[BLOCK_4X4 as usize] = Some(avsc_satd4x4_neon);
        out[BLOCK_4X8 as usize] = Some(avsc_satd4x8_neon);
        out[BLOCK_4X16 as usize] = Some(avsc_satd4x16_neon);
        out[BLOCK_8X4 as usize] = Some(avsc_satd8x4_neon);
        out[BLOCK_16X4 as usize] = Some(avsc_satd16x4_neon);

        out[BLOCK_8X8 as usize] = Some(avsc_satd8x8_neon);
        out[BLOCK_8X16 as usize] = Some(avsc_satd8x16_neon);
        out[BLOCK_8X32 as usize] = Some(avsc_satd8x32_neon);
        out[BLOCK_16X8 as usize] = Some(avsc_satd16x8_neon);
        out[BLOCK_16X16 as usize] = Some(avsc_satd16x16_neon);
        out[BLOCK_16X32 as usize] = Some(avsc_satd16x32_neon);
        out[BLOCK_16X64 as usize] = Some(avsc_satd16x64_neon);
        out[BLOCK_32X8 as usize] = Some(avsc_satd32x8_neon);
        out[BLOCK_32X16 as usize] = Some(avsc_satd32x16_neon);
        out[BLOCK_32X32 as usize] = Some(avsc_satd32x32_neon);
        out[BLOCK_32X64 as usize] = Some(avsc_satd32x64_neon);
        out[BLOCK_64X16 as usize] = Some(avsc_satd64x16_neon);
        out[BLOCK_64X32 as usize] = Some(avsc_satd64x32_neon);
        out[BLOCK_64X64 as usize] = Some(avsc_satd64x64_neon);
        out[BLOCK_64X128 as usize] = Some(avsc_satd64x128_neon);
        out[BLOCK_128X64 as usize] = Some(avsc_satd128x64_neon);
        out[BLOCK_128X128 as usize] = Some(avsc_satd128x128_neon);

        out
    };

    static SATD_HBD_FNS_NEON: [Option<SatdHbdFn>; DIST_FNS_LENGTH] = {
        let mut out: [Option<SatdHbdFn>; DIST_FNS_LENGTH] = [None; DIST_FNS_LENGTH];

        use crate::data::block::BlockSize::*;

        out[BLOCK_4X4 as usize] = Some(avsc_satd4x4_hbd_neon);
        out[BLOCK_4X8 as usize] = Some(avsc_satd4x8_hbd_neon);
        out[BLOCK_4X16 as usize] = Some(avsc_satd4x16_hbd_neon);
        out[BLOCK_8X4 as usize] = Some(avsc_satd8x4_hbd_neon);
        out[BLOCK_16X4 as usize] = Some(avsc_satd16x4_hbd_neon);

        out[BLOCK_8X8 as usize] = Some(avsc_satd8x8_hbd_neon);
        out[BLOCK_8X16 as usize] = Some(avsc_satd8x16_hbd_neon);
        out[BLOCK_8X32 as usize] = Some(avsc_satd8x32_hbd_neon);
        out[BLOCK_16X8 as usize] = Some(avsc_satd16x8_hbd_neon);
        out[BLOCK_16X16 as usize] = Some(avsc_satd16x16_hbd_neon);
        out[BLOCK_16X32 as usize] = Some(avsc_satd16x32_hbd_neon);
        out[BLOCK_16X64 as usize] = Some(avsc_satd16x64_hbd_neon);
        out[BLOCK_32X8 as usize] = Some(avsc_satd32x8_hbd_neon);
        out[BLOCK_32X16 as usize] = Some(avsc_satd32x16_hbd_neon);
        out[BLOCK_32X32 as usize] = Some(avsc_satd32x32_hbd_neon);
        out[BLOCK_32X64 as usize] = Some(avsc_satd32x64_hbd_neon);
        out[BLOCK_64X16 as usize] = Some(avsc_satd64x16_hbd_neon);
        out[BLOCK_64X32 as usize] = Some(avsc_satd64x32_hbd_neon);
        out[BLOCK_64X64 as usize] = Some(avsc_satd64x64_hbd_neon);
        out[BLOCK_64X128 as usize] = Some(avsc_satd64x128_hbd_neon);
        out[BLOCK_128X64 as usize] = Some(avsc_satd128x64_hbd_neon);
        out[BLOCK_128X128 as usize] = Some(avsc_satd128x128_hbd_neon);

        out
    };

    cpu_function_lookup_table!(
      SATD_FNS: [[Option<SatdFn>; DIST_FNS_LENGTH]],
      default: [None; DIST_FNS_LENGTH],
      [NEON]
    );

    cpu_function_lookup_table!(
      SATD_HBD_FNS: [[Option<SatdHbdFn>; DIST_FNS_LENGTH]],
      default: [None; DIST_FNS_LENGTH],
      [NEON]
    );

    pub(super) fn get_satd_internal<T: Pixel>(
        src: &PlaneRegion<'_, T>,
        dst: &PlaneRegion<'_, T>,
        w: usize,
        h: usize,
        bit_depth: usize,
        cpu: CpuFeatureLevel,
    ) -> u32 {
        let bsize_opt = BlockSize::from_width_and_height_opt(w, h);

        let call_rust =
            || -> u32 { super::rust::get_satd_internal(src, dst, w, h, bit_depth, cpu) };

        match (bsize_opt, T::type_enum()) {
            (Err(_), _) => call_rust(),
            (Ok(bsize), PixelType::U8) => {
                match SATD_FNS[cpu.as_index()][to_index(bsize)] {
                    // SAFETY: Calls Assembly code.
                    Some(func) => unsafe {
                        (func)(
                            src.data_ptr() as *const _,
                            T::to_asm_stride(src.plane_cfg.stride),
                            dst.data_ptr() as *const _,
                            T::to_asm_stride(dst.plane_cfg.stride),
                        )
                    },
                    None => call_rust(),
                }
            }
            (Ok(bsize), PixelType::U16) => {
                match SATD_HBD_FNS[cpu.as_index()][to_index(bsize)] {
                    // SAFETY: Calls Assembly code.
                    Some(func) => unsafe {
                        (func)(
                            src.data_ptr() as *const _,
                            T::to_asm_stride(src.plane_cfg.stride),
                            dst.data_ptr() as *const _,
                            T::to_asm_stride(dst.plane_cfg.stride),
                        )
                    },
                    None => call_rust(),
                }
            }
        }
    }
}

// BlockSize::BLOCK_SIZES.next_power_of_two()
const DIST_FNS_LENGTH: usize = 32;

const fn to_index(bsize: BlockSize) -> usize {
    bsize as usize & (DIST_FNS_LENGTH - 1)
}

pub(crate) fn get_satd<T: Pixel>(
    src: &PlaneRegion<'_, T>,
    dst: &PlaneRegion<'_, T>,
    w: usize,
    h: usize,
    bit_depth: usize,
    cpu: CpuFeatureLevel,
) -> u32 {
    get_satd_internal(src, dst, w, h, bit_depth, cpu)
}