1 /*
2  *  Copyright (c) 2012 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include <arm_neon.h>
12 
13 #include "vp8/encoder/denoising.h"
14 #include "vpx_mem/vpx_mem.h"
15 #include "./vp8_rtcd.h"
16 
17 /*
18  * The filter function was modified to reduce the computational complexity.
19  *
20  * Step 1:
21  *  Instead of applying tap coefficients for each pixel, we calculated the
22  *  pixel adjustments vs. pixel diff value ahead of time.
23  *     adjustment = filtered_value - current_raw
24  *                = (filter_coefficient * diff + 128) >> 8
25  *  where
26  *     filter_coefficient = (255 << 8) / (256 + ((abs_diff * 330) >> 3));
27  *     filter_coefficient += filter_coefficient /
28  *                           (3 + motion_magnitude_adjustment);
29  *     filter_coefficient is clamped to 0 ~ 255.
30  *
31  * Step 2:
32  *  The adjustment vs. diff curve becomes flat very quick when diff increases.
33  *  This allowed us to use only several levels to approximate the curve without
34  *  changing the filtering algorithm too much.
35  *  The adjustments were further corrected by checking the motion magnitude.
36  *  The levels used are:
37  *      diff          level       adjustment w/o       adjustment w/
38  *                               motion correction    motion correction
39  *      [-255, -16]     3              -6                   -7
40  *      [-15, -8]       2              -4                   -5
41  *      [-7, -4]        1              -3                   -4
42  *      [-3, 3]         0              diff                 diff
43  *      [4, 7]          1               3                    4
44  *      [8, 15]         2               4                    5
45  *      [16, 255]       3               6                    7
46  */
47 
vp8_denoiser_filter_neon(YV12_BUFFER_CONFIG * mc_running_avg,YV12_BUFFER_CONFIG * running_avg,MACROBLOCK * signal,unsigned int motion_magnitude,int y_offset,int uv_offset)48 int vp8_denoiser_filter_neon(YV12_BUFFER_CONFIG *mc_running_avg,
49                              YV12_BUFFER_CONFIG *running_avg,
50                              MACROBLOCK *signal, unsigned int motion_magnitude,
51                              int y_offset, int uv_offset) {
52     /* If motion_magnitude is small, making the denoiser more aggressive by
53      * increasing the adjustment for each level, level1 adjustment is
54      * increased, the deltas stay the same.
55      */
56     const uint8x16_t v_level1_adjustment = vdupq_n_u8(
57         (motion_magnitude <= MOTION_MAGNITUDE_THRESHOLD) ? 4 : 3);
58     const uint8x16_t v_delta_level_1_and_2 = vdupq_n_u8(1);
59     const uint8x16_t v_delta_level_2_and_3 = vdupq_n_u8(2);
60     const uint8x16_t v_level1_threshold = vdupq_n_u8(4);
61     const uint8x16_t v_level2_threshold = vdupq_n_u8(8);
62     const uint8x16_t v_level3_threshold = vdupq_n_u8(16);
63 
64     /* Local variables for array pointers and strides. */
65     unsigned char *sig = signal->thismb;
66     int            sig_stride = 16;
67     unsigned char *mc_running_avg_y = mc_running_avg->y_buffer + y_offset;
68     int            mc_running_avg_y_stride = mc_running_avg->y_stride;
69     unsigned char *running_avg_y = running_avg->y_buffer + y_offset;
70     int            running_avg_y_stride = running_avg->y_stride;
71 
72     /* Go over lines. */
73     int i;
74     int sum_diff = 0;
75     for (i = 0; i < 16; ++i) {
76         int8x16_t v_sum_diff = vdupq_n_s8(0);
77         uint8x16_t v_running_avg_y;
78 
79         /* Load inputs. */
80         const uint8x16_t v_sig = vld1q_u8(sig);
81         const uint8x16_t v_mc_running_avg_y = vld1q_u8(mc_running_avg_y);
82 
83         /* Calculate absolute difference and sign masks. */
84         const uint8x16_t v_abs_diff      = vabdq_u8(v_sig, v_mc_running_avg_y);
85         const uint8x16_t v_diff_pos_mask = vcltq_u8(v_sig, v_mc_running_avg_y);
86         const uint8x16_t v_diff_neg_mask = vcgtq_u8(v_sig, v_mc_running_avg_y);
87 
88         /* Figure out which level that put us in. */
89         const uint8x16_t v_level1_mask = vcleq_u8(v_level1_threshold,
90                                                   v_abs_diff);
91         const uint8x16_t v_level2_mask = vcleq_u8(v_level2_threshold,
92                                                   v_abs_diff);
93         const uint8x16_t v_level3_mask = vcleq_u8(v_level3_threshold,
94                                                   v_abs_diff);
95 
96         /* Calculate absolute adjustments for level 1, 2 and 3. */
97         const uint8x16_t v_level2_adjustment = vandq_u8(v_level2_mask,
98                                                         v_delta_level_1_and_2);
99         const uint8x16_t v_level3_adjustment = vandq_u8(v_level3_mask,
100                                                         v_delta_level_2_and_3);
101         const uint8x16_t v_level1and2_adjustment = vaddq_u8(v_level1_adjustment,
102             v_level2_adjustment);
103         const uint8x16_t v_level1and2and3_adjustment = vaddq_u8(
104             v_level1and2_adjustment, v_level3_adjustment);
105 
106         /* Figure adjustment absolute value by selecting between the absolute
107          * difference if in level0 or the value for level 1, 2 and 3.
108          */
109         const uint8x16_t v_abs_adjustment = vbslq_u8(v_level1_mask,
110             v_level1and2and3_adjustment, v_abs_diff);
111 
112         /* Calculate positive and negative adjustments. Apply them to the signal
113          * and accumulate them. Adjustments are less than eight and the maximum
114          * sum of them (7 * 16) can fit in a signed char.
115          */
116         const uint8x16_t v_pos_adjustment = vandq_u8(v_diff_pos_mask,
117                                                      v_abs_adjustment);
118         const uint8x16_t v_neg_adjustment = vandq_u8(v_diff_neg_mask,
119                                                      v_abs_adjustment);
120         v_running_avg_y = vqaddq_u8(v_sig, v_pos_adjustment);
121         v_running_avg_y = vqsubq_u8(v_running_avg_y, v_neg_adjustment);
122         v_sum_diff = vqaddq_s8(v_sum_diff,
123                                vreinterpretq_s8_u8(v_pos_adjustment));
124         v_sum_diff = vqsubq_s8(v_sum_diff,
125                                vreinterpretq_s8_u8(v_neg_adjustment));
126 
127         /* Store results. */
128         vst1q_u8(running_avg_y, v_running_avg_y);
129 
130         /* Sum all the accumulators to have the sum of all pixel differences
131          * for this macroblock.
132          */
133         {
134             int s0 = vgetq_lane_s8(v_sum_diff,  0) +
135                      vgetq_lane_s8(v_sum_diff,  1) +
136                      vgetq_lane_s8(v_sum_diff,  2) +
137                      vgetq_lane_s8(v_sum_diff,  3);
138             int s1 = vgetq_lane_s8(v_sum_diff,  4) +
139                      vgetq_lane_s8(v_sum_diff,  5) +
140                      vgetq_lane_s8(v_sum_diff,  6) +
141                      vgetq_lane_s8(v_sum_diff,  7);
142             int s2 = vgetq_lane_s8(v_sum_diff,  8) +
143                      vgetq_lane_s8(v_sum_diff,  9) +
144                      vgetq_lane_s8(v_sum_diff, 10) +
145                      vgetq_lane_s8(v_sum_diff, 11);
146             int s3 = vgetq_lane_s8(v_sum_diff, 12) +
147                      vgetq_lane_s8(v_sum_diff, 13) +
148                      vgetq_lane_s8(v_sum_diff, 14) +
149                      vgetq_lane_s8(v_sum_diff, 15);
150             sum_diff += s0 + s1+ s2 + s3;
151         }
152 
153         /* Update pointers for next iteration. */
154         sig += sig_stride;
155         mc_running_avg_y += mc_running_avg_y_stride;
156         running_avg_y += running_avg_y_stride;
157     }
158 
159     /* Too much adjustments => copy block. */
160     if (abs(sum_diff) > SUM_DIFF_THRESHOLD)
161         return COPY_BLOCK;
162 
163     /* Tell above level that block was filtered. */
164     vp8_copy_mem16x16(running_avg->y_buffer + y_offset, running_avg_y_stride,
165                       signal->thismb, sig_stride);
166     return FILTER_BLOCK;
167 }
168