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.
13 #include "denoising.h"
15 #include "vp8/common/reconinter.h"
16 #include "vpx/vpx_integer.h"
17 #include "vpx_mem/vpx_mem.h"
20 static const unsigned int NOISE_MOTION_THRESHOLD = 25 * 25;
21 /* SSE_DIFF_THRESHOLD is selected as ~95% confidence assuming
24 static const unsigned int SSE_DIFF_THRESHOLD = 16 * 16 * 20;
25 static const unsigned int SSE_THRESHOLD = 16 * 16 * 40;
26 static const unsigned int SSE_THRESHOLD_HIGH = 16 * 16 * 60;
29 * The filter function was modified to reduce the computational complexity.
31 * Instead of applying tap coefficients for each pixel, we calculated the
32 * pixel adjustments vs. pixel diff value ahead of time.
33 * adjustment = filtered_value - current_raw
34 * = (filter_coefficient * diff + 128) >> 8
36 * filter_coefficient = (255 << 8) / (256 + ((absdiff * 330) >> 3));
37 * filter_coefficient += filter_coefficient /
38 * (3 + motion_magnitude_adjustment);
39 * filter_coefficient is clamped to 0 ~ 255.
42 * The adjustment vs. diff curve becomes flat very quick when diff increases.
43 * This allowed us to use only several levels to approximate the curve without
44 * changing the filtering algorithm too much.
45 * The adjustments were further corrected by checking the motion magnitude.
46 * The levels used are:
47 * diff adjustment w/o motion correction adjustment w/ motion correction
57 int vp8_denoiser_filter_c(unsigned char *mc_running_avg_y, int mc_avg_y_stride,
58 unsigned char *running_avg_y, int avg_y_stride,
59 unsigned char *sig, int sig_stride,
60 unsigned int motion_magnitude,
61 int increase_denoising)
63 unsigned char *running_avg_y_start = running_avg_y;
64 unsigned char *sig_start = sig;
68 int adj_val[3] = {3, 4, 6};
71 /* If motion_magnitude is small, making the denoiser more aggressive by
72 * increasing the adjustment for each level. Add another increment for
73 * blocks that are labeled for increase denoising. */
74 if (motion_magnitude <= MOTION_MAGNITUDE_THRESHOLD)
76 if (increase_denoising) {
80 adj_val[0] += shift_inc2;
81 adj_val[1] += shift_inc2;
82 adj_val[2] += shift_inc2;
85 for (r = 0; r < 16; ++r)
87 for (c = 0; c < 16; ++c)
93 diff = mc_running_avg_y[c] - sig[c];
96 // When |diff| <= |3 + shift_inc1|, use pixel value from
98 if (absdiff <= 3 + shift_inc1)
100 running_avg_y[c] = mc_running_avg_y[c];
105 if (absdiff >= 4 && absdiff <= 7)
106 adjustment = adj_val[0];
107 else if (absdiff >= 8 && absdiff <= 15)
108 adjustment = adj_val[1];
110 adjustment = adj_val[2];
114 if ((sig[c] + adjustment) > 255)
115 running_avg_y[c] = 255;
117 running_avg_y[c] = sig[c] + adjustment;
119 sum_diff += adjustment;
123 if ((sig[c] - adjustment) < 0)
124 running_avg_y[c] = 0;
126 running_avg_y[c] = sig[c] - adjustment;
128 sum_diff -= adjustment;
133 /* Update pointers for next iteration. */
135 mc_running_avg_y += mc_avg_y_stride;
136 running_avg_y += avg_y_stride;
139 sum_diff_thresh= SUM_DIFF_THRESHOLD;
140 if (increase_denoising) sum_diff_thresh = SUM_DIFF_THRESHOLD_HIGH;
141 if (abs(sum_diff) > sum_diff_thresh) {
142 // Before returning to copy the block (i.e., apply no denoising), check
143 // if we can still apply some (weaker) temporal filtering to this block,
144 // that would otherwise not be denoised at all. Simplest is to apply
145 // an additional adjustment to running_avg_y to bring it closer to sig.
146 // The adjustment is capped by a maximum delta, and chosen such that
147 // in most cases the resulting sum_diff will be within the
148 // accceptable range given by sum_diff_thresh.
150 // The delta is set by the excess of absolute pixel diff over threshold.
151 int delta = ((abs(sum_diff) - sum_diff_thresh) >> 8) + 1;
152 // Only apply the adjustment for max delta up to 3.
154 sig -= sig_stride * 16;
155 mc_running_avg_y -= mc_avg_y_stride * 16;
156 running_avg_y -= avg_y_stride * 16;
157 for (r = 0; r < 16; ++r) {
158 for (c = 0; c < 16; ++c) {
159 int diff = mc_running_avg_y[c] - sig[c];
160 int adjustment = abs(diff);
161 if (adjustment > delta)
164 // Bring denoised signal down.
165 if (running_avg_y[c] - adjustment < 0)
166 running_avg_y[c] = 0;
168 running_avg_y[c] = running_avg_y[c] - adjustment;
169 sum_diff -= adjustment;
170 } else if (diff < 0) {
171 // Bring denoised signal up.
172 if (running_avg_y[c] + adjustment > 255)
173 running_avg_y[c] = 255;
175 running_avg_y[c] = running_avg_y[c] + adjustment;
176 sum_diff += adjustment;
179 // TODO(marpan): Check here if abs(sum_diff) has gone below the
180 // threshold sum_diff_thresh, and if so, we can exit the row loop.
182 mc_running_avg_y += mc_avg_y_stride;
183 running_avg_y += avg_y_stride;
185 if (abs(sum_diff) > sum_diff_thresh)
192 vp8_copy_mem16x16(running_avg_y_start, avg_y_stride, sig_start, sig_stride);
196 int vp8_denoiser_filter_uv_c(unsigned char *mc_running_avg_uv,
197 int mc_avg_uv_stride,
198 unsigned char *running_avg_uv,
202 unsigned int motion_magnitude,
203 int increase_denoising) {
204 unsigned char *running_avg_uv_start = running_avg_uv;
205 unsigned char *sig_start = sig;
210 int adj_val[3] = {3, 4, 6};
213 /* If motion_magnitude is small, making the denoiser more aggressive by
214 * increasing the adjustment for each level. Add another increment for
215 * blocks that are labeled for increase denoising. */
216 if (motion_magnitude <= MOTION_MAGNITUDE_THRESHOLD_UV) {
217 if (increase_denoising) {
221 adj_val[0] += shift_inc2;
222 adj_val[1] += shift_inc2;
223 adj_val[2] += shift_inc2;
226 // Avoid denoising color signal if its close to average level.
227 for (r = 0; r < 8; ++r) {
228 for (c = 0; c < 8; ++c) {
233 if (abs(sum_block - (128 * 8 * 8)) < SUM_DIFF_FROM_AVG_THRESH_UV) {
237 sig -= sig_stride * 8;
238 for (r = 0; r < 8; ++r) {
239 for (c = 0; c < 8; ++c) {
244 diff = mc_running_avg_uv[c] - sig[c];
247 // When |diff| <= |3 + shift_inc1|, use pixel value from
248 // last denoised raw.
249 if (absdiff <= 3 + shift_inc1) {
250 running_avg_uv[c] = mc_running_avg_uv[c];
253 if (absdiff >= 4 && absdiff <= 7)
254 adjustment = adj_val[0];
255 else if (absdiff >= 8 && absdiff <= 15)
256 adjustment = adj_val[1];
258 adjustment = adj_val[2];
260 if ((sig[c] + adjustment) > 255)
261 running_avg_uv[c] = 255;
263 running_avg_uv[c] = sig[c] + adjustment;
264 sum_diff += adjustment;
266 if ((sig[c] - adjustment) < 0)
267 running_avg_uv[c] = 0;
269 running_avg_uv[c] = sig[c] - adjustment;
270 sum_diff -= adjustment;
274 /* Update pointers for next iteration. */
276 mc_running_avg_uv += mc_avg_uv_stride;
277 running_avg_uv += avg_uv_stride;
280 sum_diff_thresh= SUM_DIFF_THRESHOLD_UV;
281 if (increase_denoising) sum_diff_thresh = SUM_DIFF_THRESHOLD_HIGH_UV;
282 if (abs(sum_diff) > sum_diff_thresh) {
283 // Before returning to copy the block (i.e., apply no denoising), check
284 // if we can still apply some (weaker) temporal filtering to this block,
285 // that would otherwise not be denoised at all. Simplest is to apply
286 // an additional adjustment to running_avg_y to bring it closer to sig.
287 // The adjustment is capped by a maximum delta, and chosen such that
288 // in most cases the resulting sum_diff will be within the
289 // accceptable range given by sum_diff_thresh.
291 // The delta is set by the excess of absolute pixel diff over threshold.
292 int delta = ((abs(sum_diff) - sum_diff_thresh) >> 8) + 1;
293 // Only apply the adjustment for max delta up to 3.
295 sig -= sig_stride * 8;
296 mc_running_avg_uv -= mc_avg_uv_stride * 8;
297 running_avg_uv -= avg_uv_stride * 8;
298 for (r = 0; r < 8; ++r) {
299 for (c = 0; c < 8; ++c) {
300 int diff = mc_running_avg_uv[c] - sig[c];
301 int adjustment = abs(diff);
302 if (adjustment > delta)
305 // Bring denoised signal down.
306 if (running_avg_uv[c] - adjustment < 0)
307 running_avg_uv[c] = 0;
309 running_avg_uv[c] = running_avg_uv[c] - adjustment;
310 sum_diff -= adjustment;
311 } else if (diff < 0) {
312 // Bring denoised signal up.
313 if (running_avg_uv[c] + adjustment > 255)
314 running_avg_uv[c] = 255;
316 running_avg_uv[c] = running_avg_uv[c] + adjustment;
317 sum_diff += adjustment;
320 // TODO(marpan): Check here if abs(sum_diff) has gone below the
321 // threshold sum_diff_thresh, and if so, we can exit the row loop.
323 mc_running_avg_uv += mc_avg_uv_stride;
324 running_avg_uv += avg_uv_stride;
326 if (abs(sum_diff) > sum_diff_thresh)
333 vp8_copy_mem8x8(running_avg_uv_start, avg_uv_stride, sig_start,
338 void vp8_denoiser_set_parameters(VP8_DENOISER *denoiser, int mode) {
339 assert(mode > 0); // Denoiser is allocated only if mode > 0.
341 denoiser->denoiser_mode = kDenoiserOnYOnly;
342 } else if (mode == 2) {
343 denoiser->denoiser_mode = kDenoiserOnYUV;
345 denoiser->denoiser_mode = kDenoiserOnYUVAggressive;
347 if (denoiser->denoiser_mode != kDenoiserOnYUVAggressive) {
348 denoiser->denoise_pars.scale_sse_thresh = 1;
349 denoiser->denoise_pars.scale_motion_thresh = 8;
350 denoiser->denoise_pars.scale_increase_filter = 0;
351 denoiser->denoise_pars.denoise_mv_bias = 95;
352 denoiser->denoise_pars.pickmode_mv_bias = 100;
353 denoiser->denoise_pars.qp_thresh = 0;
354 denoiser->denoise_pars.consec_zerolast = UINT_MAX;
356 denoiser->denoise_pars.scale_sse_thresh = 2;
357 denoiser->denoise_pars.scale_motion_thresh = 16;
358 denoiser->denoise_pars.scale_increase_filter = 1;
359 denoiser->denoise_pars.denoise_mv_bias = 60;
360 denoiser->denoise_pars.pickmode_mv_bias = 60;
361 denoiser->denoise_pars.qp_thresh = 100;
362 denoiser->denoise_pars.consec_zerolast = 10;
366 int vp8_denoiser_allocate(VP8_DENOISER *denoiser, int width, int height,
367 int num_mb_rows, int num_mb_cols, int mode)
371 denoiser->num_mb_cols = num_mb_cols;
373 for (i = 0; i < MAX_REF_FRAMES; i++)
375 denoiser->yv12_running_avg[i].flags = 0;
377 if (vp8_yv12_alloc_frame_buffer(&(denoiser->yv12_running_avg[i]), width,
378 height, VP8BORDERINPIXELS)
381 vp8_denoiser_free(denoiser);
384 vpx_memset(denoiser->yv12_running_avg[i].buffer_alloc, 0,
385 denoiser->yv12_running_avg[i].frame_size);
388 denoiser->yv12_mc_running_avg.flags = 0;
390 if (vp8_yv12_alloc_frame_buffer(&(denoiser->yv12_mc_running_avg), width,
391 height, VP8BORDERINPIXELS) < 0)
393 vp8_denoiser_free(denoiser);
397 vpx_memset(denoiser->yv12_mc_running_avg.buffer_alloc, 0,
398 denoiser->yv12_mc_running_avg.frame_size);
400 denoiser->denoise_state = vpx_calloc((num_mb_rows * num_mb_cols), 1);
401 vpx_memset(denoiser->denoise_state, 0, (num_mb_rows * num_mb_cols));
402 vp8_denoiser_set_parameters(denoiser, mode);
407 void vp8_denoiser_free(VP8_DENOISER *denoiser)
412 for (i = 0; i < MAX_REF_FRAMES ; i++)
414 vp8_yv12_de_alloc_frame_buffer(&denoiser->yv12_running_avg[i]);
416 vp8_yv12_de_alloc_frame_buffer(&denoiser->yv12_mc_running_avg);
417 vpx_free(denoiser->denoise_state);
421 void vp8_denoiser_denoise_mb(VP8_DENOISER *denoiser,
423 unsigned int best_sse,
424 unsigned int zero_mv_sse,
427 loop_filter_info_n *lfi_n,
435 unsigned int motion_threshold;
436 unsigned int motion_magnitude2;
437 unsigned int sse_thresh;
438 int sse_diff_thresh = 0;
439 // Spatial loop filter: only applied selectively based on
440 // temporal filter state of block relative to top/left neighbors.
441 int apply_spatial_loop_filter = 1;
442 MV_REFERENCE_FRAME frame = x->best_reference_frame;
443 MV_REFERENCE_FRAME zero_frame = x->best_zeromv_reference_frame;
445 enum vp8_denoiser_decision decision = FILTER_BLOCK;
446 enum vp8_denoiser_decision decision_u = COPY_BLOCK;
447 enum vp8_denoiser_decision decision_v = COPY_BLOCK;
451 YV12_BUFFER_CONFIG *src = &denoiser->yv12_running_avg[frame];
452 YV12_BUFFER_CONFIG *dst = &denoiser->yv12_mc_running_avg;
453 YV12_BUFFER_CONFIG saved_pre,saved_dst;
454 MB_MODE_INFO saved_mbmi;
455 MACROBLOCKD *filter_xd = &x->e_mbd;
456 MB_MODE_INFO *mbmi = &filter_xd->mode_info_context->mbmi;
458 // Bias on zero motion vector sse.
459 const int zero_bias = denoiser->denoise_pars.denoise_mv_bias;
460 zero_mv_sse = (unsigned int)((int64_t)zero_mv_sse * zero_bias / 100);
461 sse_diff = zero_mv_sse - best_sse;
465 /* Use the best MV for the compensation. */
466 mbmi->ref_frame = x->best_reference_frame;
467 mbmi->mode = x->best_sse_inter_mode;
468 mbmi->mv = x->best_sse_mv;
469 mbmi->need_to_clamp_mvs = x->need_to_clamp_best_mvs;
470 mv_col = x->best_sse_mv.as_mv.col;
471 mv_row = x->best_sse_mv.as_mv.row;
472 // Bias to zero_mv if small amount of motion.
473 // Note sse_diff_thresh is intialized to zero, so this ensures
474 // we will always choose zero_mv for denoising if
475 // zero_mv_see <= best_sse (i.e., sse_diff <= 0).
476 if ((unsigned int)(mv_row * mv_row + mv_col * mv_col)
477 <= NOISE_MOTION_THRESHOLD)
478 sse_diff_thresh = (int)SSE_DIFF_THRESHOLD;
480 if (frame == INTRA_FRAME ||
481 sse_diff <= sse_diff_thresh)
484 * Handle intra blocks as referring to last frame with zero motion
485 * and let the absolute pixel difference affect the filter factor.
486 * Also consider small amount of motion as being random walk due
487 * to noise, if it doesn't mean that we get a much bigger error.
488 * Note that any changes to the mode info only affects the
492 x->best_zeromv_reference_frame;
494 src = &denoiser->yv12_running_avg[zero_frame];
498 x->best_sse_inter_mode = ZEROMV;
499 x->best_sse_mv.as_int = 0;
500 best_sse = zero_mv_sse;
503 saved_pre = filter_xd->pre;
504 saved_dst = filter_xd->dst;
506 /* Compensate the running average. */
507 filter_xd->pre.y_buffer = src->y_buffer + recon_yoffset;
508 filter_xd->pre.u_buffer = src->u_buffer + recon_uvoffset;
509 filter_xd->pre.v_buffer = src->v_buffer + recon_uvoffset;
510 /* Write the compensated running average to the destination buffer. */
511 filter_xd->dst.y_buffer = dst->y_buffer + recon_yoffset;
512 filter_xd->dst.u_buffer = dst->u_buffer + recon_uvoffset;
513 filter_xd->dst.v_buffer = dst->v_buffer + recon_uvoffset;
517 vp8_build_inter_predictors_mb(filter_xd);
521 vp8_build_inter16x16_predictors_mb(filter_xd,
522 filter_xd->dst.y_buffer,
523 filter_xd->dst.u_buffer,
524 filter_xd->dst.v_buffer,
525 filter_xd->dst.y_stride,
526 filter_xd->dst.uv_stride);
528 filter_xd->pre = saved_pre;
529 filter_xd->dst = saved_dst;
534 mv_row = x->best_sse_mv.as_mv.row;
535 mv_col = x->best_sse_mv.as_mv.col;
536 motion_magnitude2 = mv_row * mv_row + mv_col * mv_col;
537 motion_threshold = denoiser->denoise_pars.scale_motion_thresh *
538 NOISE_MOTION_THRESHOLD;
540 if (motion_magnitude2 <
541 denoiser->denoise_pars.scale_increase_filter * NOISE_MOTION_THRESHOLD)
542 x->increase_denoising = 1;
544 sse_thresh = denoiser->denoise_pars.scale_sse_thresh * SSE_THRESHOLD;
545 if (x->increase_denoising)
546 sse_thresh = denoiser->denoise_pars.scale_sse_thresh * SSE_THRESHOLD_HIGH;
548 if (best_sse > sse_thresh || motion_magnitude2 > motion_threshold)
549 decision = COPY_BLOCK;
551 if (decision == FILTER_BLOCK)
553 unsigned char *mc_running_avg_y =
554 denoiser->yv12_mc_running_avg.y_buffer + recon_yoffset;
555 int mc_avg_y_stride = denoiser->yv12_mc_running_avg.y_stride;
556 unsigned char *running_avg_y =
557 denoiser->yv12_running_avg[INTRA_FRAME].y_buffer + recon_yoffset;
558 int avg_y_stride = denoiser->yv12_running_avg[INTRA_FRAME].y_stride;
561 decision = vp8_denoiser_filter(mc_running_avg_y, mc_avg_y_stride,
562 running_avg_y, avg_y_stride,
563 x->thismb, 16, motion_magnitude2,
564 x->increase_denoising);
565 denoiser->denoise_state[block_index] = motion_magnitude2 > 0 ?
566 kFilterNonZeroMV : kFilterZeroMV;
567 // Only denoise UV for zero motion, and if y channel was denoised.
568 if (denoiser->denoiser_mode != kDenoiserOnYOnly &&
569 motion_magnitude2 == 0 &&
570 decision == FILTER_BLOCK) {
571 unsigned char *mc_running_avg_u =
572 denoiser->yv12_mc_running_avg.u_buffer + recon_uvoffset;
573 unsigned char *running_avg_u =
574 denoiser->yv12_running_avg[INTRA_FRAME].u_buffer + recon_uvoffset;
575 unsigned char *mc_running_avg_v =
576 denoiser->yv12_mc_running_avg.v_buffer + recon_uvoffset;
577 unsigned char *running_avg_v =
578 denoiser->yv12_running_avg[INTRA_FRAME].v_buffer + recon_uvoffset;
579 int mc_avg_uv_stride = denoiser->yv12_mc_running_avg.uv_stride;
580 int avg_uv_stride = denoiser->yv12_running_avg[INTRA_FRAME].uv_stride;
581 int signal_stride = x->block[16].src_stride;
583 vp8_denoiser_filter_uv(mc_running_avg_u, mc_avg_uv_stride,
584 running_avg_u, avg_uv_stride,
585 x->block[16].src + *x->block[16].base_src,
586 signal_stride, motion_magnitude2, 0);
588 vp8_denoiser_filter_uv(mc_running_avg_v, mc_avg_uv_stride,
589 running_avg_v, avg_uv_stride,
590 x->block[20].src + *x->block[20].base_src,
591 signal_stride, motion_magnitude2, 0);
594 if (decision == COPY_BLOCK)
596 /* No filtering of this block; it differs too much from the predictor,
597 * or the motion vector magnitude is considered too big.
601 denoiser->yv12_running_avg[INTRA_FRAME].y_buffer + recon_yoffset,
602 denoiser->yv12_running_avg[INTRA_FRAME].y_stride);
603 denoiser->denoise_state[block_index] = kNoFilter;
605 if (denoiser->denoiser_mode != kDenoiserOnYOnly) {
606 if (decision_u == COPY_BLOCK) {
608 x->block[16].src + *x->block[16].base_src, x->block[16].src_stride,
609 denoiser->yv12_running_avg[INTRA_FRAME].u_buffer + recon_uvoffset,
610 denoiser->yv12_running_avg[INTRA_FRAME].uv_stride);
612 if (decision_v == COPY_BLOCK) {
614 x->block[20].src + *x->block[20].base_src, x->block[16].src_stride,
615 denoiser->yv12_running_avg[INTRA_FRAME].v_buffer + recon_uvoffset,
616 denoiser->yv12_running_avg[INTRA_FRAME].uv_stride);
619 // Option to selectively deblock the denoised signal, for y channel only.
620 if (apply_spatial_loop_filter) {
621 loop_filter_info lfi;
622 int apply_filter_col = 0;
623 int apply_filter_row = 0;
624 int apply_filter = 0;
625 int y_stride = denoiser->yv12_running_avg[INTRA_FRAME].y_stride;
626 int uv_stride =denoiser->yv12_running_avg[INTRA_FRAME].uv_stride;
628 // Fix filter level to some nominal value for now.
629 int filter_level = 32;
631 int hev_index = lfi_n->hev_thr_lut[INTER_FRAME][filter_level];
632 lfi.mblim = lfi_n->mblim[filter_level];
633 lfi.blim = lfi_n->blim[filter_level];
634 lfi.lim = lfi_n->lim[filter_level];
635 lfi.hev_thr = lfi_n->hev_thr[hev_index];
637 // Apply filter if there is a difference in the denoiser filter state
638 // between the current and left/top block, or if non-zero motion vector
639 // is used for the motion-compensated filtering.
641 apply_filter_col = !((denoiser->denoise_state[block_index] ==
642 denoiser->denoise_state[block_index - 1]) &&
643 denoiser->denoise_state[block_index] != kFilterNonZeroMV);
644 if (apply_filter_col) {
645 // Filter left vertical edge.
648 denoiser->yv12_running_avg[INTRA_FRAME].y_buffer + recon_yoffset,
649 NULL, NULL, y_stride, uv_stride, &lfi);
653 apply_filter_row = !((denoiser->denoise_state[block_index] ==
654 denoiser->denoise_state[block_index - denoiser->num_mb_cols]) &&
655 denoiser->denoise_state[block_index] != kFilterNonZeroMV);
656 if (apply_filter_row) {
657 // Filter top horizontal edge.
660 denoiser->yv12_running_avg[INTRA_FRAME].y_buffer + recon_yoffset,
661 NULL, NULL, y_stride, uv_stride, &lfi);
665 // Update the signal block |x|. Pixel changes are only to top and/or
666 // left boundary pixels: can we avoid full block copy here.
668 denoiser->yv12_running_avg[INTRA_FRAME].y_buffer + recon_yoffset,
669 y_stride, x->thismb, 16);