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
//! Does loop filtering on webp lossy images

use crate::utils::clamp;

#[inline]
fn c(val: i32) -> i32 {
    clamp(val, -128, 127)
}

//unsigned to signed
#[inline]
fn u2s(val: u8) -> i32 {
    i32::from(val) - 128
}

//signed to unsigned
#[inline]
fn s2u(val: i32) -> u8 {
    (c(val) + 128) as u8
}

#[inline]
fn diff(val1: u8, val2: u8) -> u8 {
    if val1 > val2 {
        val1 - val2
    } else {
        val2 - val1
    }
}

//15.2
fn common_adjust(use_outer_taps: bool, pixels: &mut [u8], point: usize, stride: usize) -> i32 {
    let p1 = u2s(pixels[point - 2 * stride]);
    let p0 = u2s(pixels[point - stride]);
    let q0 = u2s(pixels[point]);
    let q1 = u2s(pixels[point + stride]);

    //value for the outer 2 pixels
    let outer = if use_outer_taps { c(p1 - q1) } else { 0 };

    let mut a = c(outer + 3 * (q0 - p0));

    let b = (c(a + 3)) >> 3;

    a = (c(a + 4)) >> 3;

    pixels[point] = s2u(q0 - a);
    pixels[point - stride] = s2u(p0 + b);

    a
}

fn simple_threshold(filter_limit: i32, pixels: &[u8], point: usize, stride: usize) -> bool {
    i32::from(diff(pixels[point - stride], pixels[point])) * 2
        + i32::from(diff(pixels[point - 2 * stride], pixels[point + stride])) / 2
        <= filter_limit
}

fn should_filter(
    interior_limit: u8,
    edge_limit: u8,
    pixels: &[u8],
    point: usize,
    stride: usize,
) -> bool {
    simple_threshold(i32::from(edge_limit), pixels, point, stride)
        && diff(pixels[point - 4 * stride], pixels[point - 3 * stride]) <= interior_limit
        && diff(pixels[point - 3 * stride], pixels[point - 2 * stride]) <= interior_limit
        && diff(pixels[point - 2 * stride], pixels[point - stride]) <= interior_limit
        && diff(pixels[point + 3 * stride], pixels[point + 2 * stride]) <= interior_limit
        && diff(pixels[point + 2 * stride], pixels[point + stride]) <= interior_limit
        && diff(pixels[point + stride], pixels[point]) <= interior_limit
}

fn high_edge_variance(threshold: u8, pixels: &[u8], point: usize, stride: usize) -> bool {
    diff(pixels[point - 2 * stride], pixels[point - stride]) > threshold
        || diff(pixels[point + stride], pixels[point]) > threshold
}

//simple filter
//effects 4 pixels on an edge(2 each side)
pub(crate) fn simple_segment(edge_limit: u8, pixels: &mut [u8], point: usize, stride: usize) {
    if simple_threshold(i32::from(edge_limit), pixels, point, stride) {
        common_adjust(true, pixels, point, stride);
    }
}

//normal filter
//works on the 8 pixels on the edges between subblocks inside a macroblock
pub(crate) fn subblock_filter(
    hev_threshold: u8,
    interior_limit: u8,
    edge_limit: u8,
    pixels: &mut [u8],
    point: usize,
    stride: usize,
) {
    if should_filter(interior_limit, edge_limit, pixels, point, stride) {
        let hv = high_edge_variance(hev_threshold, pixels, point, stride);

        let a = (common_adjust(hv, pixels, point, stride) + 1) >> 1;

        if !hv {
            pixels[point + stride] = s2u(u2s(pixels[point + stride]) - a);
            pixels[point - 2 * stride] = s2u(u2s(pixels[point - 2 * stride]) - a);
        }
    }
}

//normal filter
//works on the 8 pixels on the edges between macroblocks
pub(crate) fn macroblock_filter(
    hev_threshold: u8,
    interior_limit: u8,
    edge_limit: u8,
    pixels: &mut [u8],
    point: usize,
    stride: usize,
) {
    let mut spixels = [0i32; 8];
    for i in 0..8 {
        spixels[i] = u2s(pixels[point + i * stride - 4 * stride]);
    }

    if should_filter(interior_limit, edge_limit, pixels, point, stride) {
        if !high_edge_variance(hev_threshold, pixels, point, stride) {
            let w = c(c(spixels[2] - spixels[5]) + 3 * (spixels[4] - spixels[3]));

            let mut a = c((27 * w + 63) >> 7);

            pixels[point] = s2u(spixels[4] - a);
            pixels[point - stride] = s2u(spixels[3] + a);

            a = c((18 * w + 63) >> 7);

            pixels[point + stride] = s2u(spixels[5] - a);
            pixels[point - 2 * stride] = s2u(spixels[2] + a);

            a = c((9 * w + 63) >> 7);

            pixels[point + 2 * stride] = s2u(spixels[6] - a);
            pixels[point - 3 * stride] = s2u(spixels[1] + a);
        } else {
            common_adjust(true, pixels, point, stride);
        }
    }
}