1 /*
2 * Copyright (c) 2014 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 <emmintrin.h>
12
13 #include "./vpx_config.h"
14 #include "./vp9_rtcd.h"
15
16 #include "vpx_ports/emmintrin_compat.h"
17 #include "vpx/vpx_integer.h"
18 #include "vp9/common/vp9_reconinter.h"
19 #include "vp9/encoder/vp9_context_tree.h"
20 #include "vp9/encoder/vp9_denoiser.h"
21 #include "vpx_mem/vpx_mem.h"
22
23 // Compute the sum of all pixel differences of this MB.
sum_diff_16x1(__m128i acc_diff)24 static INLINE int sum_diff_16x1(__m128i acc_diff) {
25 const __m128i k_1 = _mm_set1_epi16(1);
26 const __m128i acc_diff_lo =
27 _mm_srai_epi16(_mm_unpacklo_epi8(acc_diff, acc_diff), 8);
28 const __m128i acc_diff_hi =
29 _mm_srai_epi16(_mm_unpackhi_epi8(acc_diff, acc_diff), 8);
30 const __m128i acc_diff_16 = _mm_add_epi16(acc_diff_lo, acc_diff_hi);
31 const __m128i hg_fe_dc_ba = _mm_madd_epi16(acc_diff_16, k_1);
32 const __m128i hgfe_dcba =
33 _mm_add_epi32(hg_fe_dc_ba, _mm_srli_si128(hg_fe_dc_ba, 8));
34 const __m128i hgfedcba =
35 _mm_add_epi32(hgfe_dcba, _mm_srli_si128(hgfe_dcba, 4));
36 return _mm_cvtsi128_si32(hgfedcba);
37 }
38
39 // Denoise a 16x1 vector.
vp9_denoiser_16x1_sse2(const uint8_t * sig,const uint8_t * mc_running_avg_y,uint8_t * running_avg_y,const __m128i * k_0,const __m128i * k_4,const __m128i * k_8,const __m128i * k_16,const __m128i * l3,const __m128i * l32,const __m128i * l21,__m128i acc_diff)40 static INLINE __m128i vp9_denoiser_16x1_sse2(const uint8_t *sig,
41 const uint8_t *mc_running_avg_y,
42 uint8_t *running_avg_y,
43 const __m128i *k_0,
44 const __m128i *k_4,
45 const __m128i *k_8,
46 const __m128i *k_16,
47 const __m128i *l3,
48 const __m128i *l32,
49 const __m128i *l21,
50 __m128i acc_diff) {
51 // Calculate differences
52 const __m128i v_sig = _mm_loadu_si128((const __m128i *)(&sig[0]));
53 const __m128i v_mc_running_avg_y =
54 _mm_loadu_si128((const __m128i *)(&mc_running_avg_y[0]));
55 __m128i v_running_avg_y;
56 const __m128i pdiff = _mm_subs_epu8(v_mc_running_avg_y, v_sig);
57 const __m128i ndiff = _mm_subs_epu8(v_sig, v_mc_running_avg_y);
58 // Obtain the sign. FF if diff is negative.
59 const __m128i diff_sign = _mm_cmpeq_epi8(pdiff, *k_0);
60 // Clamp absolute difference to 16 to be used to get mask. Doing this
61 // allows us to use _mm_cmpgt_epi8, which operates on signed byte.
62 const __m128i clamped_absdiff =
63 _mm_min_epu8(_mm_or_si128(pdiff, ndiff), *k_16);
64 // Get masks for l2 l1 and l0 adjustments.
65 const __m128i mask2 = _mm_cmpgt_epi8(*k_16, clamped_absdiff);
66 const __m128i mask1 = _mm_cmpgt_epi8(*k_8, clamped_absdiff);
67 const __m128i mask0 = _mm_cmpgt_epi8(*k_4, clamped_absdiff);
68 // Get adjustments for l2, l1, and l0.
69 __m128i adj2 = _mm_and_si128(mask2, *l32);
70 const __m128i adj1 = _mm_and_si128(mask1, *l21);
71 const __m128i adj0 = _mm_and_si128(mask0, clamped_absdiff);
72 __m128i adj, padj, nadj;
73
74 // Combine the adjustments and get absolute adjustments.
75 adj2 = _mm_add_epi8(adj2, adj1);
76 adj = _mm_sub_epi8(*l3, adj2);
77 adj = _mm_andnot_si128(mask0, adj);
78 adj = _mm_or_si128(adj, adj0);
79
80 // Restore the sign and get positive and negative adjustments.
81 padj = _mm_andnot_si128(diff_sign, adj);
82 nadj = _mm_and_si128(diff_sign, adj);
83
84 // Calculate filtered value.
85 v_running_avg_y = _mm_adds_epu8(v_sig, padj);
86 v_running_avg_y = _mm_subs_epu8(v_running_avg_y, nadj);
87 _mm_storeu_si128((__m128i *)running_avg_y, v_running_avg_y);
88
89 // Adjustments <=7, and each element in acc_diff can fit in signed
90 // char.
91 acc_diff = _mm_adds_epi8(acc_diff, padj);
92 acc_diff = _mm_subs_epi8(acc_diff, nadj);
93 return acc_diff;
94 }
95
96 // Denoise a 16x1 vector with a weaker filter.
vp9_denoiser_adj_16x1_sse2(const uint8_t * sig,const uint8_t * mc_running_avg_y,uint8_t * running_avg_y,const __m128i k_0,const __m128i k_delta,__m128i acc_diff)97 static INLINE __m128i vp9_denoiser_adj_16x1_sse2(
98 const uint8_t *sig, const uint8_t *mc_running_avg_y,
99 uint8_t *running_avg_y, const __m128i k_0,
100 const __m128i k_delta, __m128i acc_diff) {
101 __m128i v_running_avg_y = _mm_loadu_si128((__m128i *)(&running_avg_y[0]));
102 // Calculate differences.
103 const __m128i v_sig = _mm_loadu_si128((const __m128i *)(&sig[0]));
104 const __m128i v_mc_running_avg_y =
105 _mm_loadu_si128((const __m128i *)(&mc_running_avg_y[0]));
106 const __m128i pdiff = _mm_subs_epu8(v_mc_running_avg_y, v_sig);
107 const __m128i ndiff = _mm_subs_epu8(v_sig, v_mc_running_avg_y);
108 // Obtain the sign. FF if diff is negative.
109 const __m128i diff_sign = _mm_cmpeq_epi8(pdiff, k_0);
110 // Clamp absolute difference to delta to get the adjustment.
111 const __m128i adj =
112 _mm_min_epu8(_mm_or_si128(pdiff, ndiff), k_delta);
113 // Restore the sign and get positive and negative adjustments.
114 __m128i padj, nadj;
115 padj = _mm_andnot_si128(diff_sign, adj);
116 nadj = _mm_and_si128(diff_sign, adj);
117 // Calculate filtered value.
118 v_running_avg_y = _mm_subs_epu8(v_running_avg_y, padj);
119 v_running_avg_y = _mm_adds_epu8(v_running_avg_y, nadj);
120 _mm_storeu_si128((__m128i *)running_avg_y, v_running_avg_y);
121
122 // Accumulate the adjustments.
123 acc_diff = _mm_subs_epi8(acc_diff, padj);
124 acc_diff = _mm_adds_epi8(acc_diff, nadj);
125 return acc_diff;
126 }
127
128 // Denoiser for 4xM and 8xM blocks.
vp9_denoiser_NxM_sse2_small(const uint8_t * sig,int sig_stride,const uint8_t * mc_running_avg_y,int mc_avg_y_stride,uint8_t * running_avg_y,int avg_y_stride,int increase_denoising,BLOCK_SIZE bs,int motion_magnitude,int width)129 static int vp9_denoiser_NxM_sse2_small(
130 const uint8_t *sig, int sig_stride, const uint8_t *mc_running_avg_y,
131 int mc_avg_y_stride, uint8_t *running_avg_y, int avg_y_stride,
132 int increase_denoising, BLOCK_SIZE bs, int motion_magnitude, int width) {
133 int sum_diff_thresh, r, sum_diff = 0;
134 const int shift_inc = (increase_denoising &&
135 motion_magnitude <= MOTION_MAGNITUDE_THRESHOLD) ?
136 1 : 0;
137 uint8_t sig_buffer[8][16], mc_running_buffer[8][16], running_buffer[8][16];
138 __m128i acc_diff = _mm_setzero_si128();
139 const __m128i k_0 = _mm_setzero_si128();
140 const __m128i k_4 = _mm_set1_epi8(4 + shift_inc);
141 const __m128i k_8 = _mm_set1_epi8(8);
142 const __m128i k_16 = _mm_set1_epi8(16);
143 // Modify each level's adjustment according to motion_magnitude.
144 const __m128i l3 = _mm_set1_epi8(
145 (motion_magnitude <= MOTION_MAGNITUDE_THRESHOLD) ? 7 + shift_inc : 6);
146 // Difference between level 3 and level 2 is 2.
147 const __m128i l32 = _mm_set1_epi8(2);
148 // Difference between level 2 and level 1 is 1.
149 const __m128i l21 = _mm_set1_epi8(1);
150 const uint8_t shift = (width == 4) ? 2 : 1;
151
152 for (r = 0; r < ((4 << b_height_log2_lookup[bs]) >> shift); ++r) {
153 memcpy(sig_buffer[r], sig, width);
154 memcpy(sig_buffer[r] + width, sig + sig_stride, width);
155 memcpy(mc_running_buffer[r], mc_running_avg_y, width);
156 memcpy(mc_running_buffer[r] + width,
157 mc_running_avg_y + mc_avg_y_stride, width);
158 memcpy(running_buffer[r], running_avg_y, width);
159 memcpy(running_buffer[r] + width, running_avg_y + avg_y_stride, width);
160 if (width == 4) {
161 memcpy(sig_buffer[r] + width * 2, sig + sig_stride * 2, width);
162 memcpy(sig_buffer[r] + width * 3, sig + sig_stride * 3, width);
163 memcpy(mc_running_buffer[r] + width * 2,
164 mc_running_avg_y + mc_avg_y_stride * 2, width);
165 memcpy(mc_running_buffer[r] + width * 3,
166 mc_running_avg_y + mc_avg_y_stride * 3, width);
167 memcpy(running_buffer[r] + width * 2,
168 running_avg_y + avg_y_stride * 2, width);
169 memcpy(running_buffer[r] + width * 3,
170 running_avg_y + avg_y_stride * 3, width);
171 }
172 acc_diff = vp9_denoiser_16x1_sse2(sig_buffer[r],
173 mc_running_buffer[r],
174 running_buffer[r],
175 &k_0, &k_4, &k_8, &k_16,
176 &l3, &l32, &l21, acc_diff);
177 memcpy(running_avg_y, running_buffer[r], width);
178 memcpy(running_avg_y + avg_y_stride, running_buffer[r] + width, width);
179 if (width == 4) {
180 memcpy(running_avg_y + avg_y_stride * 2,
181 running_buffer[r] + width * 2, width);
182 memcpy(running_avg_y + avg_y_stride * 3,
183 running_buffer[r] + width * 3, width);
184 }
185 // Update pointers for next iteration.
186 sig += (sig_stride << shift);
187 mc_running_avg_y += (mc_avg_y_stride << shift);
188 running_avg_y += (avg_y_stride << shift);
189 }
190
191 {
192 sum_diff = sum_diff_16x1(acc_diff);
193 sum_diff_thresh = total_adj_strong_thresh(bs, increase_denoising);
194 if (abs(sum_diff) > sum_diff_thresh) {
195 // Before returning to copy the block (i.e., apply no denoising),
196 // check if we can still apply some (weaker) temporal filtering to
197 // this block, that would otherwise not be denoised at all. Simplest
198 // is to apply an additional adjustment to running_avg_y to bring it
199 // closer to sig. The adjustment is capped by a maximum delta, and
200 // chosen such that in most cases the resulting sum_diff will be
201 // within the acceptable range given by sum_diff_thresh.
202
203 // The delta is set by the excess of absolute pixel diff over the
204 // threshold.
205 const int delta = ((abs(sum_diff) - sum_diff_thresh) >>
206 num_pels_log2_lookup[bs]) + 1;
207 // Only apply the adjustment for max delta up to 3.
208 if (delta < 4) {
209 const __m128i k_delta = _mm_set1_epi8(delta);
210 running_avg_y -= avg_y_stride * (4 << b_height_log2_lookup[bs]);
211 for (r = 0; r < ((4 << b_height_log2_lookup[bs]) >> shift); ++r) {
212 acc_diff = vp9_denoiser_adj_16x1_sse2(
213 sig_buffer[r], mc_running_buffer[r], running_buffer[r],
214 k_0, k_delta, acc_diff);
215 memcpy(running_avg_y, running_buffer[r], width);
216 memcpy(running_avg_y + avg_y_stride,
217 running_buffer[r] + width, width);
218 if (width == 4) {
219 memcpy(running_avg_y + avg_y_stride * 2,
220 running_buffer[r] + width * 2, width);
221 memcpy(running_avg_y + avg_y_stride * 3,
222 running_buffer[r] + width * 3, width);
223 }
224 // Update pointers for next iteration.
225 running_avg_y += (avg_y_stride << shift);
226 }
227 sum_diff = sum_diff_16x1(acc_diff);
228 if (abs(sum_diff) > sum_diff_thresh) {
229 return COPY_BLOCK;
230 }
231 } else {
232 return COPY_BLOCK;
233 }
234 }
235 }
236 return FILTER_BLOCK;
237 }
238
239 // Denoiser for 16xM, 32xM and 64xM blocks
vp9_denoiser_NxM_sse2_big(const uint8_t * sig,int sig_stride,const uint8_t * mc_running_avg_y,int mc_avg_y_stride,uint8_t * running_avg_y,int avg_y_stride,int increase_denoising,BLOCK_SIZE bs,int motion_magnitude)240 static int vp9_denoiser_NxM_sse2_big(const uint8_t *sig, int sig_stride,
241 const uint8_t *mc_running_avg_y,
242 int mc_avg_y_stride,
243 uint8_t *running_avg_y,
244 int avg_y_stride,
245 int increase_denoising, BLOCK_SIZE bs,
246 int motion_magnitude) {
247 int sum_diff_thresh, r, c, sum_diff = 0;
248 const int shift_inc = (increase_denoising &&
249 motion_magnitude <= MOTION_MAGNITUDE_THRESHOLD) ?
250 1 : 0;
251 __m128i acc_diff[4][4];
252 const __m128i k_0 = _mm_setzero_si128();
253 const __m128i k_4 = _mm_set1_epi8(4 + shift_inc);
254 const __m128i k_8 = _mm_set1_epi8(8);
255 const __m128i k_16 = _mm_set1_epi8(16);
256 // Modify each level's adjustment according to motion_magnitude.
257 const __m128i l3 = _mm_set1_epi8(
258 (motion_magnitude <= MOTION_MAGNITUDE_THRESHOLD) ? 7 + shift_inc : 6);
259 // Difference between level 3 and level 2 is 2.
260 const __m128i l32 = _mm_set1_epi8(2);
261 // Difference between level 2 and level 1 is 1.
262 const __m128i l21 = _mm_set1_epi8(1);
263
264 for (c = 0; c < 4; ++c) {
265 for (r = 0; r < 4; ++r) {
266 acc_diff[c][r] = _mm_setzero_si128();
267 }
268 }
269
270 for (r = 0; r < (4 << b_height_log2_lookup[bs]); ++r) {
271 for (c = 0; c < (4 << b_width_log2_lookup[bs]); c += 16) {
272 acc_diff[c>>4][r>>4] = vp9_denoiser_16x1_sse2(
273 sig, mc_running_avg_y, running_avg_y, &k_0, &k_4,
274 &k_8, &k_16, &l3, &l32, &l21, acc_diff[c>>4][r>>4]);
275 // Update pointers for next iteration.
276 sig += 16;
277 mc_running_avg_y += 16;
278 running_avg_y += 16;
279 }
280
281 if ((r + 1) % 16 == 0 || (bs == BLOCK_16X8 && r == 7)) {
282 for (c = 0; c < (4 << b_width_log2_lookup[bs]); c += 16) {
283 sum_diff += sum_diff_16x1(acc_diff[c>>4][r>>4]);
284 }
285 }
286
287 // Update pointers for next iteration.
288 sig = sig - 16 * ((4 << b_width_log2_lookup[bs]) >> 4) + sig_stride;
289 mc_running_avg_y = mc_running_avg_y -
290 16 * ((4 << b_width_log2_lookup[bs]) >> 4) +
291 mc_avg_y_stride;
292 running_avg_y = running_avg_y -
293 16 * ((4 << b_width_log2_lookup[bs]) >> 4) +
294 avg_y_stride;
295 }
296
297 {
298 sum_diff_thresh = total_adj_strong_thresh(bs, increase_denoising);
299 if (abs(sum_diff) > sum_diff_thresh) {
300 const int delta = ((abs(sum_diff) - sum_diff_thresh) >>
301 num_pels_log2_lookup[bs]) + 1;
302
303 // Only apply the adjustment for max delta up to 3.
304 if (delta < 4) {
305 const __m128i k_delta = _mm_set1_epi8(delta);
306 sig -= sig_stride * (4 << b_height_log2_lookup[bs]);
307 mc_running_avg_y -= mc_avg_y_stride * (4 << b_height_log2_lookup[bs]);
308 running_avg_y -= avg_y_stride * (4 << b_height_log2_lookup[bs]);
309 sum_diff = 0;
310 for (r = 0; r < (4 << b_height_log2_lookup[bs]); ++r) {
311 for (c = 0; c < (4 << b_width_log2_lookup[bs]); c += 16) {
312 acc_diff[c>>4][r>>4] = vp9_denoiser_adj_16x1_sse2(
313 sig, mc_running_avg_y, running_avg_y, k_0,
314 k_delta, acc_diff[c>>4][r>>4]);
315 // Update pointers for next iteration.
316 sig += 16;
317 mc_running_avg_y += 16;
318 running_avg_y += 16;
319 }
320
321 if ((r + 1) % 16 == 0 || (bs == BLOCK_16X8 && r == 7)) {
322 for (c = 0; c < (4 << b_width_log2_lookup[bs]); c += 16) {
323 sum_diff += sum_diff_16x1(acc_diff[c>>4][r>>4]);
324 }
325 }
326 sig = sig - 16 * ((4 << b_width_log2_lookup[bs]) >> 4) + sig_stride;
327 mc_running_avg_y = mc_running_avg_y -
328 16 * ((4 << b_width_log2_lookup[bs]) >> 4) +
329 mc_avg_y_stride;
330 running_avg_y = running_avg_y -
331 16 * ((4 << b_width_log2_lookup[bs]) >> 4) +
332 avg_y_stride;
333 }
334 if (abs(sum_diff) > sum_diff_thresh) {
335 return COPY_BLOCK;
336 }
337 } else {
338 return COPY_BLOCK;
339 }
340 }
341 }
342 return FILTER_BLOCK;
343 }
344
vp9_denoiser_filter_sse2(const uint8_t * sig,int sig_stride,const uint8_t * mc_avg,int mc_avg_stride,uint8_t * avg,int avg_stride,int increase_denoising,BLOCK_SIZE bs,int motion_magnitude)345 int vp9_denoiser_filter_sse2(const uint8_t *sig, int sig_stride,
346 const uint8_t *mc_avg,
347 int mc_avg_stride,
348 uint8_t *avg, int avg_stride,
349 int increase_denoising,
350 BLOCK_SIZE bs,
351 int motion_magnitude) {
352 if (bs == BLOCK_4X4 || bs == BLOCK_4X8) {
353 return vp9_denoiser_NxM_sse2_small(sig, sig_stride,
354 mc_avg, mc_avg_stride,
355 avg, avg_stride,
356 increase_denoising,
357 bs, motion_magnitude, 4);
358 } else if (bs == BLOCK_8X4 || bs == BLOCK_8X8 || bs == BLOCK_8X16) {
359 return vp9_denoiser_NxM_sse2_small(sig, sig_stride,
360 mc_avg, mc_avg_stride,
361 avg, avg_stride,
362 increase_denoising,
363 bs, motion_magnitude, 8);
364 } else if (bs == BLOCK_16X8 || bs == BLOCK_16X16 || bs == BLOCK_16X32 ||
365 bs == BLOCK_32X16|| bs == BLOCK_32X32 || bs == BLOCK_32X64 ||
366 bs == BLOCK_64X32 || bs == BLOCK_64X64) {
367 return vp9_denoiser_NxM_sse2_big(sig, sig_stride,
368 mc_avg, mc_avg_stride,
369 avg, avg_stride,
370 increase_denoising,
371 bs, motion_magnitude);
372 } else {
373 return COPY_BLOCK;
374 }
375 }
376