2 * Copyright (c) 2012 The WebM project authors. All Rights Reserved.
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.
11 #include "denoising.h"
13 #include "vp8/common/reconinter.h"
14 #include "vpx/vpx_integer.h"
15 #include "vpx_mem/vpx_mem.h"
18 static const unsigned int NOISE_MOTION_THRESHOLD = 25 * 25;
19 /* SSE_DIFF_THRESHOLD is selected as ~95% confidence assuming
22 static const unsigned int SSE_DIFF_THRESHOLD = 16 * 16 * 20;
23 static const unsigned int SSE_THRESHOLD = 16 * 16 * 40;
24 static const unsigned int SSE_THRESHOLD_HIGH = 16 * 16 * 60;
27 * The filter function was modified to reduce the computational complexity.
29 * Instead of applying tap coefficients for each pixel, we calculated the
30 * pixel adjustments vs. pixel diff value ahead of time.
31 * adjustment = filtered_value - current_raw
32 * = (filter_coefficient * diff + 128) >> 8
34 * filter_coefficient = (255 << 8) / (256 + ((absdiff * 330) >> 3));
35 * filter_coefficient += filter_coefficient /
36 * (3 + motion_magnitude_adjustment);
37 * filter_coefficient is clamped to 0 ~ 255.
40 * The adjustment vs. diff curve becomes flat very quick when diff increases.
41 * This allowed us to use only several levels to approximate the curve without
42 * changing the filtering algorithm too much.
43 * The adjustments were further corrected by checking the motion magnitude.
44 * The levels used are:
45 * diff adjustment w/o motion correction adjustment w/ motion correction
55 int vp8_denoiser_filter_c(unsigned char *mc_running_avg_y, int mc_avg_y_stride,
56 unsigned char *running_avg_y, int avg_y_stride,
57 unsigned char *sig, int sig_stride,
58 unsigned int motion_magnitude,
59 int increase_denoising)
61 unsigned char *running_avg_y_start = running_avg_y;
62 unsigned char *sig_start = sig;
66 int adj_val[3] = {3, 4, 6};
69 /* If motion_magnitude is small, making the denoiser more aggressive by
70 * increasing the adjustment for each level. Add another increment for
71 * blocks that are labeled for increase denoising. */
72 if (motion_magnitude <= MOTION_MAGNITUDE_THRESHOLD)
74 if (increase_denoising) {
78 adj_val[0] += shift_inc2;
79 adj_val[1] += shift_inc2;
80 adj_val[2] += shift_inc2;
83 for (r = 0; r < 16; ++r)
85 for (c = 0; c < 16; ++c)
91 diff = mc_running_avg_y[c] - sig[c];
94 // When |diff| <= |3 + shift_inc1|, use pixel value from
96 if (absdiff <= 3 + shift_inc1)
98 running_avg_y[c] = mc_running_avg_y[c];
103 if (absdiff >= 4 && absdiff <= 7)
104 adjustment = adj_val[0];
105 else if (absdiff >= 8 && absdiff <= 15)
106 adjustment = adj_val[1];
108 adjustment = adj_val[2];
112 if ((sig[c] + adjustment) > 255)
113 running_avg_y[c] = 255;
115 running_avg_y[c] = sig[c] + adjustment;
117 sum_diff += adjustment;
121 if ((sig[c] - adjustment) < 0)
122 running_avg_y[c] = 0;
124 running_avg_y[c] = sig[c] - adjustment;
126 sum_diff -= adjustment;
131 /* Update pointers for next iteration. */
133 mc_running_avg_y += mc_avg_y_stride;
134 running_avg_y += avg_y_stride;
137 sum_diff_thresh= SUM_DIFF_THRESHOLD;
138 if (increase_denoising) sum_diff_thresh = SUM_DIFF_THRESHOLD_HIGH;
139 if (abs(sum_diff) > sum_diff_thresh) {
140 // Before returning to copy the block (i.e., apply no denoising), check
141 // if we can still apply some (weaker) temporal filtering to this block,
142 // that would otherwise not be denoised at all. Simplest is to apply
143 // an additional adjustment to running_avg_y to bring it closer to sig.
144 // The adjustment is capped by a maximum delta, and chosen such that
145 // in most cases the resulting sum_diff will be within the
146 // accceptable range given by sum_diff_thresh.
148 // The delta is set by the excess of absolute pixel diff over threshold.
149 int delta = ((abs(sum_diff) - sum_diff_thresh) >> 8) + 1;
150 // Only apply the adjustment for max delta up to 3.
152 sig -= sig_stride * 16;
153 mc_running_avg_y -= mc_avg_y_stride * 16;
154 running_avg_y -= avg_y_stride * 16;
155 for (r = 0; r < 16; ++r) {
156 for (c = 0; c < 16; ++c) {
157 int diff = mc_running_avg_y[c] - sig[c];
158 int adjustment = abs(diff);
159 if (adjustment > delta)
162 // Bring denoised signal down.
163 if (running_avg_y[c] - adjustment < 0)
164 running_avg_y[c] = 0;
166 running_avg_y[c] = running_avg_y[c] - adjustment;
167 sum_diff -= adjustment;
168 } else if (diff < 0) {
169 // Bring denoised signal up.
170 if (running_avg_y[c] + adjustment > 255)
171 running_avg_y[c] = 255;
173 running_avg_y[c] = running_avg_y[c] + adjustment;
174 sum_diff += adjustment;
177 // TODO(marpan): Check here if abs(sum_diff) has gone below the
178 // threshold sum_diff_thresh, and if so, we can exit the row loop.
180 mc_running_avg_y += mc_avg_y_stride;
181 running_avg_y += avg_y_stride;
183 if (abs(sum_diff) > sum_diff_thresh)
190 vp8_copy_mem16x16(running_avg_y_start, avg_y_stride, sig_start, sig_stride);
194 int vp8_denoiser_allocate(VP8_DENOISER *denoiser, int width, int height,
195 int num_mb_rows, int num_mb_cols)
199 denoiser->num_mb_cols = num_mb_cols;
201 for (i = 0; i < MAX_REF_FRAMES; i++)
203 denoiser->yv12_running_avg[i].flags = 0;
205 if (vp8_yv12_alloc_frame_buffer(&(denoiser->yv12_running_avg[i]), width,
206 height, VP8BORDERINPIXELS)
209 vp8_denoiser_free(denoiser);
212 vpx_memset(denoiser->yv12_running_avg[i].buffer_alloc, 0,
213 denoiser->yv12_running_avg[i].frame_size);
216 denoiser->yv12_mc_running_avg.flags = 0;
218 if (vp8_yv12_alloc_frame_buffer(&(denoiser->yv12_mc_running_avg), width,
219 height, VP8BORDERINPIXELS) < 0)
221 vp8_denoiser_free(denoiser);
225 vpx_memset(denoiser->yv12_mc_running_avg.buffer_alloc, 0,
226 denoiser->yv12_mc_running_avg.frame_size);
228 denoiser->denoise_state = vpx_calloc((num_mb_rows * num_mb_cols), 1);
229 vpx_memset(denoiser->denoise_state, 0, (num_mb_rows * num_mb_cols));
234 void vp8_denoiser_free(VP8_DENOISER *denoiser)
239 for (i = 0; i < MAX_REF_FRAMES ; i++)
241 vp8_yv12_de_alloc_frame_buffer(&denoiser->yv12_running_avg[i]);
243 vp8_yv12_de_alloc_frame_buffer(&denoiser->yv12_mc_running_avg);
247 void vp8_denoiser_denoise_mb(VP8_DENOISER *denoiser,
249 unsigned int best_sse,
250 unsigned int zero_mv_sse,
253 loop_filter_info_n *lfi_n,
260 unsigned int motion_magnitude2;
261 unsigned int sse_thresh;
262 int sse_diff_thresh = 0;
263 // Spatial loop filter: only applied selectively based on
264 // temporal filter state of block relative to top/left neighbors.
265 int apply_spatial_loop_filter = 1;
266 MV_REFERENCE_FRAME frame = x->best_reference_frame;
267 MV_REFERENCE_FRAME zero_frame = x->best_zeromv_reference_frame;
269 enum vp8_denoiser_decision decision = FILTER_BLOCK;
273 YV12_BUFFER_CONFIG *src = &denoiser->yv12_running_avg[frame];
274 YV12_BUFFER_CONFIG *dst = &denoiser->yv12_mc_running_avg;
275 YV12_BUFFER_CONFIG saved_pre,saved_dst;
276 MB_MODE_INFO saved_mbmi;
277 MACROBLOCKD *filter_xd = &x->e_mbd;
278 MB_MODE_INFO *mbmi = &filter_xd->mode_info_context->mbmi;
280 // Bias on zero motion vector sse.
282 zero_mv_sse = (unsigned int)((int64_t)zero_mv_sse * zero_bias / 100);
283 sse_diff = zero_mv_sse - best_sse;
287 /* Use the best MV for the compensation. */
288 mbmi->ref_frame = x->best_reference_frame;
289 mbmi->mode = x->best_sse_inter_mode;
290 mbmi->mv = x->best_sse_mv;
291 mbmi->need_to_clamp_mvs = x->need_to_clamp_best_mvs;
292 mv_col = x->best_sse_mv.as_mv.col;
293 mv_row = x->best_sse_mv.as_mv.row;
294 // Bias to zero_mv if small amount of motion.
295 // Note sse_diff_thresh is intialized to zero, so this ensures
296 // we will always choose zero_mv for denoising if
297 // zero_mv_see <= best_sse (i.e., sse_diff <= 0).
298 if ((unsigned int)(mv_row * mv_row + mv_col * mv_col)
299 <= NOISE_MOTION_THRESHOLD)
300 sse_diff_thresh = (int)SSE_DIFF_THRESHOLD;
302 if (frame == INTRA_FRAME ||
303 sse_diff <= sse_diff_thresh)
306 * Handle intra blocks as referring to last frame with zero motion
307 * and let the absolute pixel difference affect the filter factor.
308 * Also consider small amount of motion as being random walk due
309 * to noise, if it doesn't mean that we get a much bigger error.
310 * Note that any changes to the mode info only affects the
314 x->best_zeromv_reference_frame;
316 src = &denoiser->yv12_running_avg[zero_frame];
320 x->best_sse_inter_mode = ZEROMV;
321 x->best_sse_mv.as_int = 0;
322 best_sse = zero_mv_sse;
325 saved_pre = filter_xd->pre;
326 saved_dst = filter_xd->dst;
328 /* Compensate the running average. */
329 filter_xd->pre.y_buffer = src->y_buffer + recon_yoffset;
330 filter_xd->pre.u_buffer = src->u_buffer + recon_uvoffset;
331 filter_xd->pre.v_buffer = src->v_buffer + recon_uvoffset;
332 /* Write the compensated running average to the destination buffer. */
333 filter_xd->dst.y_buffer = dst->y_buffer + recon_yoffset;
334 filter_xd->dst.u_buffer = dst->u_buffer + recon_uvoffset;
335 filter_xd->dst.v_buffer = dst->v_buffer + recon_uvoffset;
339 vp8_build_inter_predictors_mb(filter_xd);
343 vp8_build_inter16x16_predictors_mb(filter_xd,
344 filter_xd->dst.y_buffer,
345 filter_xd->dst.u_buffer,
346 filter_xd->dst.v_buffer,
347 filter_xd->dst.y_stride,
348 filter_xd->dst.uv_stride);
350 filter_xd->pre = saved_pre;
351 filter_xd->dst = saved_dst;
356 mv_row = x->best_sse_mv.as_mv.row;
357 mv_col = x->best_sse_mv.as_mv.col;
358 motion_magnitude2 = mv_row * mv_row + mv_col * mv_col;
359 sse_thresh = SSE_THRESHOLD;
360 if (x->increase_denoising) sse_thresh = SSE_THRESHOLD_HIGH;
362 if (best_sse > sse_thresh || motion_magnitude2
363 > 8 * NOISE_MOTION_THRESHOLD)
365 decision = COPY_BLOCK;
368 if (decision == FILTER_BLOCK)
370 unsigned char *mc_running_avg_y =
371 denoiser->yv12_mc_running_avg.y_buffer + recon_yoffset;
372 int mc_avg_y_stride = denoiser->yv12_mc_running_avg.y_stride;
373 unsigned char *running_avg_y =
374 denoiser->yv12_running_avg[INTRA_FRAME].y_buffer + recon_yoffset;
375 int avg_y_stride = denoiser->yv12_running_avg[INTRA_FRAME].y_stride;
378 decision = vp8_denoiser_filter(mc_running_avg_y, mc_avg_y_stride,
379 running_avg_y, avg_y_stride,
380 x->thismb, 16, motion_magnitude2,
381 x->increase_denoising);
382 denoiser->denoise_state[block_index] = motion_magnitude2 > 0 ?
383 kFilterNonZeroMV : kFilterZeroMV;
385 if (decision == COPY_BLOCK)
387 /* No filtering of this block; it differs too much from the predictor,
388 * or the motion vector magnitude is considered too big.
392 denoiser->yv12_running_avg[INTRA_FRAME].y_buffer + recon_yoffset,
393 denoiser->yv12_running_avg[INTRA_FRAME].y_stride);
394 denoiser->denoise_state[block_index] = kNoFilter;
396 // Option to selectively deblock the denoised signal.
397 if (apply_spatial_loop_filter) {
398 loop_filter_info lfi;
399 int apply_filter_col = 0;
400 int apply_filter_row = 0;
401 int apply_filter = 0;
402 int y_stride = denoiser->yv12_running_avg[INTRA_FRAME].y_stride;
403 int uv_stride =denoiser->yv12_running_avg[INTRA_FRAME].uv_stride;
405 // Fix filter level to some nominal value for now.
406 int filter_level = 32;
408 int hev_index = lfi_n->hev_thr_lut[INTER_FRAME][filter_level];
409 lfi.mblim = lfi_n->mblim[filter_level];
410 lfi.blim = lfi_n->blim[filter_level];
411 lfi.lim = lfi_n->lim[filter_level];
412 lfi.hev_thr = lfi_n->hev_thr[hev_index];
414 // Apply filter if there is a difference in the denoiser filter state
415 // between the current and left/top block, or if non-zero motion vector
416 // is used for the motion-compensated filtering.
418 apply_filter_col = !((denoiser->denoise_state[block_index] ==
419 denoiser->denoise_state[block_index - 1]) &&
420 denoiser->denoise_state[block_index] != kFilterNonZeroMV);
421 if (apply_filter_col) {
422 // Filter left vertical edge.
425 denoiser->yv12_running_avg[INTRA_FRAME].y_buffer + recon_yoffset,
426 NULL, NULL, y_stride, uv_stride, &lfi);
430 apply_filter_row = !((denoiser->denoise_state[block_index] ==
431 denoiser->denoise_state[block_index - denoiser->num_mb_cols]) &&
432 denoiser->denoise_state[block_index] != kFilterNonZeroMV);
433 if (apply_filter_row) {
434 // Filter top horizontal edge.
437 denoiser->yv12_running_avg[INTRA_FRAME].y_buffer + recon_yoffset,
438 NULL, NULL, y_stride, uv_stride, &lfi);
442 // Update the signal block |x|. Pixel changes are only to top and/or
443 // left boundary pixels: can we avoid full block copy here.
445 denoiser->yv12_running_avg[INTRA_FRAME].y_buffer + recon_yoffset,
446 y_stride, x->thismb, 16);