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_filter_uv_c(unsigned char *mc_running_avg_uv,
195 int mc_avg_uv_stride,
196 unsigned char *running_avg_uv,
200 unsigned int motion_magnitude,
201 int increase_denoising) {
202 unsigned char *running_avg_uv_start = running_avg_uv;
203 unsigned char *sig_start = sig;
208 int adj_val[3] = {3, 4, 6};
211 /* If motion_magnitude is small, making the denoiser more aggressive by
212 * increasing the adjustment for each level. Add another increment for
213 * blocks that are labeled for increase denoising. */
214 if (motion_magnitude <= MOTION_MAGNITUDE_THRESHOLD_UV) {
215 if (increase_denoising) {
219 adj_val[0] += shift_inc2;
220 adj_val[1] += shift_inc2;
221 adj_val[2] += shift_inc2;
224 // Avoid denoising color signal if its close to average level.
225 for (r = 0; r < 8; ++r) {
226 for (c = 0; c < 8; ++c) {
231 if (abs(sum_block - (128 * 8 * 8)) < SUM_DIFF_FROM_AVG_THRESH_UV) {
235 sig -= sig_stride * 8;
236 for (r = 0; r < 8; ++r) {
237 for (c = 0; c < 8; ++c) {
242 diff = mc_running_avg_uv[c] - sig[c];
245 // When |diff| <= |3 + shift_inc1|, use pixel value from
246 // last denoised raw.
247 if (absdiff <= 3 + shift_inc1) {
248 running_avg_uv[c] = mc_running_avg_uv[c];
251 if (absdiff >= 4 && absdiff <= 7)
252 adjustment = adj_val[0];
253 else if (absdiff >= 8 && absdiff <= 15)
254 adjustment = adj_val[1];
256 adjustment = adj_val[2];
258 if ((sig[c] + adjustment) > 255)
259 running_avg_uv[c] = 255;
261 running_avg_uv[c] = sig[c] + adjustment;
262 sum_diff += adjustment;
264 if ((sig[c] - adjustment) < 0)
265 running_avg_uv[c] = 0;
267 running_avg_uv[c] = sig[c] - adjustment;
268 sum_diff -= adjustment;
272 /* Update pointers for next iteration. */
274 mc_running_avg_uv += mc_avg_uv_stride;
275 running_avg_uv += avg_uv_stride;
278 sum_diff_thresh= SUM_DIFF_THRESHOLD_UV;
279 if (increase_denoising) sum_diff_thresh = SUM_DIFF_THRESHOLD_HIGH_UV;
280 if (abs(sum_diff) > sum_diff_thresh) {
281 // Before returning to copy the block (i.e., apply no denoising), check
282 // if we can still apply some (weaker) temporal filtering to this block,
283 // that would otherwise not be denoised at all. Simplest is to apply
284 // an additional adjustment to running_avg_y to bring it closer to sig.
285 // The adjustment is capped by a maximum delta, and chosen such that
286 // in most cases the resulting sum_diff will be within the
287 // accceptable range given by sum_diff_thresh.
289 // The delta is set by the excess of absolute pixel diff over threshold.
290 int delta = ((abs(sum_diff) - sum_diff_thresh) >> 8) + 1;
291 // Only apply the adjustment for max delta up to 3.
293 sig -= sig_stride * 8;
294 mc_running_avg_uv -= mc_avg_uv_stride * 8;
295 running_avg_uv -= avg_uv_stride * 8;
296 for (r = 0; r < 8; ++r) {
297 for (c = 0; c < 8; ++c) {
298 int diff = mc_running_avg_uv[c] - sig[c];
299 int adjustment = abs(diff);
300 if (adjustment > delta)
303 // Bring denoised signal down.
304 if (running_avg_uv[c] - adjustment < 0)
305 running_avg_uv[c] = 0;
307 running_avg_uv[c] = running_avg_uv[c] - adjustment;
308 sum_diff -= adjustment;
309 } else if (diff < 0) {
310 // Bring denoised signal up.
311 if (running_avg_uv[c] + adjustment > 255)
312 running_avg_uv[c] = 255;
314 running_avg_uv[c] = running_avg_uv[c] + adjustment;
315 sum_diff += adjustment;
318 // TODO(marpan): Check here if abs(sum_diff) has gone below the
319 // threshold sum_diff_thresh, and if so, we can exit the row loop.
321 mc_running_avg_uv += mc_avg_uv_stride;
322 running_avg_uv += avg_uv_stride;
324 if (abs(sum_diff) > sum_diff_thresh)
331 vp8_copy_mem8x8(running_avg_uv_start, avg_uv_stride, sig_start,
336 int vp8_denoiser_allocate(VP8_DENOISER *denoiser, int width, int height,
337 int num_mb_rows, int num_mb_cols)
341 denoiser->num_mb_cols = num_mb_cols;
343 for (i = 0; i < MAX_REF_FRAMES; i++)
345 denoiser->yv12_running_avg[i].flags = 0;
347 if (vp8_yv12_alloc_frame_buffer(&(denoiser->yv12_running_avg[i]), width,
348 height, VP8BORDERINPIXELS)
351 vp8_denoiser_free(denoiser);
354 vpx_memset(denoiser->yv12_running_avg[i].buffer_alloc, 0,
355 denoiser->yv12_running_avg[i].frame_size);
358 denoiser->yv12_mc_running_avg.flags = 0;
360 if (vp8_yv12_alloc_frame_buffer(&(denoiser->yv12_mc_running_avg), width,
361 height, VP8BORDERINPIXELS) < 0)
363 vp8_denoiser_free(denoiser);
367 vpx_memset(denoiser->yv12_mc_running_avg.buffer_alloc, 0,
368 denoiser->yv12_mc_running_avg.frame_size);
370 denoiser->denoise_state = vpx_calloc((num_mb_rows * num_mb_cols), 1);
371 vpx_memset(denoiser->denoise_state, 0, (num_mb_rows * num_mb_cols));
376 void vp8_denoiser_free(VP8_DENOISER *denoiser)
381 for (i = 0; i < MAX_REF_FRAMES ; i++)
383 vp8_yv12_de_alloc_frame_buffer(&denoiser->yv12_running_avg[i]);
385 vp8_yv12_de_alloc_frame_buffer(&denoiser->yv12_mc_running_avg);
386 vpx_free(denoiser->denoise_state);
390 void vp8_denoiser_denoise_mb(VP8_DENOISER *denoiser,
392 unsigned int best_sse,
393 unsigned int zero_mv_sse,
396 loop_filter_info_n *lfi_n,
404 unsigned int motion_magnitude2;
405 unsigned int sse_thresh;
406 int sse_diff_thresh = 0;
407 // Spatial loop filter: only applied selectively based on
408 // temporal filter state of block relative to top/left neighbors.
409 int apply_spatial_loop_filter = 1;
410 MV_REFERENCE_FRAME frame = x->best_reference_frame;
411 MV_REFERENCE_FRAME zero_frame = x->best_zeromv_reference_frame;
413 enum vp8_denoiser_decision decision = FILTER_BLOCK;
414 enum vp8_denoiser_decision decision_u = FILTER_BLOCK;
415 enum vp8_denoiser_decision decision_v = FILTER_BLOCK;
419 YV12_BUFFER_CONFIG *src = &denoiser->yv12_running_avg[frame];
420 YV12_BUFFER_CONFIG *dst = &denoiser->yv12_mc_running_avg;
421 YV12_BUFFER_CONFIG saved_pre,saved_dst;
422 MB_MODE_INFO saved_mbmi;
423 MACROBLOCKD *filter_xd = &x->e_mbd;
424 MB_MODE_INFO *mbmi = &filter_xd->mode_info_context->mbmi;
426 // Bias on zero motion vector sse.
428 zero_mv_sse = (unsigned int)((int64_t)zero_mv_sse * zero_bias / 100);
429 sse_diff = zero_mv_sse - best_sse;
433 /* Use the best MV for the compensation. */
434 mbmi->ref_frame = x->best_reference_frame;
435 mbmi->mode = x->best_sse_inter_mode;
436 mbmi->mv = x->best_sse_mv;
437 mbmi->need_to_clamp_mvs = x->need_to_clamp_best_mvs;
438 mv_col = x->best_sse_mv.as_mv.col;
439 mv_row = x->best_sse_mv.as_mv.row;
440 // Bias to zero_mv if small amount of motion.
441 // Note sse_diff_thresh is intialized to zero, so this ensures
442 // we will always choose zero_mv for denoising if
443 // zero_mv_see <= best_sse (i.e., sse_diff <= 0).
444 if ((unsigned int)(mv_row * mv_row + mv_col * mv_col)
445 <= NOISE_MOTION_THRESHOLD)
446 sse_diff_thresh = (int)SSE_DIFF_THRESHOLD;
448 if (frame == INTRA_FRAME ||
449 sse_diff <= sse_diff_thresh)
452 * Handle intra blocks as referring to last frame with zero motion
453 * and let the absolute pixel difference affect the filter factor.
454 * Also consider small amount of motion as being random walk due
455 * to noise, if it doesn't mean that we get a much bigger error.
456 * Note that any changes to the mode info only affects the
460 x->best_zeromv_reference_frame;
462 src = &denoiser->yv12_running_avg[zero_frame];
466 x->best_sse_inter_mode = ZEROMV;
467 x->best_sse_mv.as_int = 0;
468 best_sse = zero_mv_sse;
471 saved_pre = filter_xd->pre;
472 saved_dst = filter_xd->dst;
474 /* Compensate the running average. */
475 filter_xd->pre.y_buffer = src->y_buffer + recon_yoffset;
476 filter_xd->pre.u_buffer = src->u_buffer + recon_uvoffset;
477 filter_xd->pre.v_buffer = src->v_buffer + recon_uvoffset;
478 /* Write the compensated running average to the destination buffer. */
479 filter_xd->dst.y_buffer = dst->y_buffer + recon_yoffset;
480 filter_xd->dst.u_buffer = dst->u_buffer + recon_uvoffset;
481 filter_xd->dst.v_buffer = dst->v_buffer + recon_uvoffset;
485 vp8_build_inter_predictors_mb(filter_xd);
489 vp8_build_inter16x16_predictors_mb(filter_xd,
490 filter_xd->dst.y_buffer,
491 filter_xd->dst.u_buffer,
492 filter_xd->dst.v_buffer,
493 filter_xd->dst.y_stride,
494 filter_xd->dst.uv_stride);
496 filter_xd->pre = saved_pre;
497 filter_xd->dst = saved_dst;
502 mv_row = x->best_sse_mv.as_mv.row;
503 mv_col = x->best_sse_mv.as_mv.col;
504 motion_magnitude2 = mv_row * mv_row + mv_col * mv_col;
505 sse_thresh = SSE_THRESHOLD;
506 if (x->increase_denoising) sse_thresh = SSE_THRESHOLD_HIGH;
508 if (best_sse > sse_thresh || motion_magnitude2
509 > 8 * NOISE_MOTION_THRESHOLD)
511 decision = COPY_BLOCK;
514 if (decision == FILTER_BLOCK)
516 unsigned char *mc_running_avg_y =
517 denoiser->yv12_mc_running_avg.y_buffer + recon_yoffset;
518 int mc_avg_y_stride = denoiser->yv12_mc_running_avg.y_stride;
519 unsigned char *running_avg_y =
520 denoiser->yv12_running_avg[INTRA_FRAME].y_buffer + recon_yoffset;
521 int avg_y_stride = denoiser->yv12_running_avg[INTRA_FRAME].y_stride;
524 decision = vp8_denoiser_filter(mc_running_avg_y, mc_avg_y_stride,
525 running_avg_y, avg_y_stride,
526 x->thismb, 16, motion_magnitude2,
527 x->increase_denoising);
528 denoiser->denoise_state[block_index] = motion_magnitude2 > 0 ?
529 kFilterNonZeroMV : kFilterZeroMV;
530 // Only denoise UV for zero motion, and if y channel was denoised.
532 motion_magnitude2 == 0 &&
533 decision == FILTER_BLOCK) {
534 unsigned char *mc_running_avg_u =
535 denoiser->yv12_mc_running_avg.u_buffer + recon_uvoffset;
536 unsigned char *running_avg_u =
537 denoiser->yv12_running_avg[INTRA_FRAME].u_buffer + recon_uvoffset;
538 unsigned char *mc_running_avg_v =
539 denoiser->yv12_mc_running_avg.v_buffer + recon_uvoffset;
540 unsigned char *running_avg_v =
541 denoiser->yv12_running_avg[INTRA_FRAME].v_buffer + recon_uvoffset;
542 int mc_avg_uv_stride = denoiser->yv12_mc_running_avg.uv_stride;
543 int avg_uv_stride = denoiser->yv12_running_avg[INTRA_FRAME].uv_stride;
544 int signal_stride = x->block[16].src_stride;
546 vp8_denoiser_filter_uv(mc_running_avg_u, mc_avg_uv_stride,
547 running_avg_u, avg_uv_stride,
548 x->block[16].src + *x->block[16].base_src,
549 signal_stride, motion_magnitude2, 0);
551 vp8_denoiser_filter_uv(mc_running_avg_v, mc_avg_uv_stride,
552 running_avg_v, avg_uv_stride,
553 x->block[20].src + *x->block[20].base_src,
554 signal_stride, motion_magnitude2, 0);
557 if (decision == COPY_BLOCK)
559 /* No filtering of this block; it differs too much from the predictor,
560 * or the motion vector magnitude is considered too big.
564 denoiser->yv12_running_avg[INTRA_FRAME].y_buffer + recon_yoffset,
565 denoiser->yv12_running_avg[INTRA_FRAME].y_stride);
566 denoiser->denoise_state[block_index] = kNoFilter;
569 if (decision_u == COPY_BLOCK) {
571 x->block[16].src + *x->block[16].base_src, x->block[16].src_stride,
572 denoiser->yv12_running_avg[INTRA_FRAME].u_buffer + recon_uvoffset,
573 denoiser->yv12_running_avg[INTRA_FRAME].uv_stride);
575 if (decision_v == COPY_BLOCK) {
577 x->block[20].src + *x->block[20].base_src, x->block[16].src_stride,
578 denoiser->yv12_running_avg[INTRA_FRAME].v_buffer + recon_uvoffset,
579 denoiser->yv12_running_avg[INTRA_FRAME].uv_stride);
582 // Option to selectively deblock the denoised signal, for y channel only.
583 if (apply_spatial_loop_filter) {
584 loop_filter_info lfi;
585 int apply_filter_col = 0;
586 int apply_filter_row = 0;
587 int apply_filter = 0;
588 int y_stride = denoiser->yv12_running_avg[INTRA_FRAME].y_stride;
589 int uv_stride =denoiser->yv12_running_avg[INTRA_FRAME].uv_stride;
591 // Fix filter level to some nominal value for now.
592 int filter_level = 32;
594 int hev_index = lfi_n->hev_thr_lut[INTER_FRAME][filter_level];
595 lfi.mblim = lfi_n->mblim[filter_level];
596 lfi.blim = lfi_n->blim[filter_level];
597 lfi.lim = lfi_n->lim[filter_level];
598 lfi.hev_thr = lfi_n->hev_thr[hev_index];
600 // Apply filter if there is a difference in the denoiser filter state
601 // between the current and left/top block, or if non-zero motion vector
602 // is used for the motion-compensated filtering.
604 apply_filter_col = !((denoiser->denoise_state[block_index] ==
605 denoiser->denoise_state[block_index - 1]) &&
606 denoiser->denoise_state[block_index] != kFilterNonZeroMV);
607 if (apply_filter_col) {
608 // Filter left vertical edge.
611 denoiser->yv12_running_avg[INTRA_FRAME].y_buffer + recon_yoffset,
612 NULL, NULL, y_stride, uv_stride, &lfi);
616 apply_filter_row = !((denoiser->denoise_state[block_index] ==
617 denoiser->denoise_state[block_index - denoiser->num_mb_cols]) &&
618 denoiser->denoise_state[block_index] != kFilterNonZeroMV);
619 if (apply_filter_row) {
620 // Filter top horizontal edge.
623 denoiser->yv12_running_avg[INTRA_FRAME].y_buffer + recon_yoffset,
624 NULL, NULL, y_stride, uv_stride, &lfi);
628 // Update the signal block |x|. Pixel changes are only to top and/or
629 // left boundary pixels: can we avoid full block copy here.
631 denoiser->yv12_running_avg[INTRA_FRAME].y_buffer + recon_yoffset,
632 y_stride, x->thismb, 16);