• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <limits.h>
12 
13 #include "denoising.h"
14 
15 #include "vp8/common/reconinter.h"
16 #include "vpx/vpx_integer.h"
17 #include "vpx_mem/vpx_mem.h"
18 #include "vp8_rtcd.h"
19 
20 static const unsigned int NOISE_MOTION_THRESHOLD = 25 * 25;
21 /* SSE_DIFF_THRESHOLD is selected as ~95% confidence assuming
22  * var(noise) ~= 100.
23  */
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 * 80;
27 
28 /*
29  * The filter function was modified to reduce the computational complexity.
30  * Step 1:
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
35  * where
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.
40  *
41  * Step 2:
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
48  * [-255, -16]           -6                                   -7
49  * [-15, -8]             -4                                   -5
50  * [-7, -4]              -3                                   -4
51  * [-3, 3]               diff                                 diff
52  * [4, 7]                 3                                    4
53  * [8, 15]                4                                    5
54  * [16, 255]              6                                    7
55  */
56 
vp8_denoiser_filter_c(unsigned char * mc_running_avg_y,int mc_avg_y_stride,unsigned char * running_avg_y,int avg_y_stride,unsigned char * sig,int sig_stride,unsigned int motion_magnitude,int increase_denoising)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) {
62   unsigned char *running_avg_y_start = running_avg_y;
63   unsigned char *sig_start = sig;
64   int sum_diff_thresh;
65   int r, c;
66   int sum_diff = 0;
67   int adj_val[3] = { 3, 4, 6 };
68   int shift_inc1 = 0;
69   int shift_inc2 = 1;
70   int col_sum[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
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) {
75     if (increase_denoising) {
76       shift_inc1 = 1;
77       shift_inc2 = 2;
78     }
79     adj_val[0] += shift_inc2;
80     adj_val[1] += shift_inc2;
81     adj_val[2] += shift_inc2;
82   }
83 
84   for (r = 0; r < 16; ++r) {
85     for (c = 0; c < 16; ++c) {
86       int diff = 0;
87       int adjustment = 0;
88       int absdiff = 0;
89 
90       diff = mc_running_avg_y[c] - sig[c];
91       absdiff = abs(diff);
92 
93       // When |diff| <= |3 + shift_inc1|, use pixel value from
94       // last denoised raw.
95       if (absdiff <= 3 + shift_inc1) {
96         running_avg_y[c] = mc_running_avg_y[c];
97         col_sum[c] += diff;
98       } else {
99         if (absdiff >= 4 + shift_inc1 && absdiff <= 7) {
100           adjustment = adj_val[0];
101         } else if (absdiff >= 8 && absdiff <= 15) {
102           adjustment = adj_val[1];
103         } else {
104           adjustment = adj_val[2];
105         }
106 
107         if (diff > 0) {
108           if ((sig[c] + adjustment) > 255) {
109             running_avg_y[c] = 255;
110           } else {
111             running_avg_y[c] = sig[c] + adjustment;
112           }
113 
114           col_sum[c] += adjustment;
115         } else {
116           if ((sig[c] - adjustment) < 0) {
117             running_avg_y[c] = 0;
118           } else {
119             running_avg_y[c] = sig[c] - adjustment;
120           }
121 
122           col_sum[c] -= adjustment;
123         }
124       }
125     }
126 
127     /* Update pointers for next iteration. */
128     sig += sig_stride;
129     mc_running_avg_y += mc_avg_y_stride;
130     running_avg_y += avg_y_stride;
131   }
132 
133   for (c = 0; c < 16; ++c) {
134     // Below we clip the value in the same way which SSE code use.
135     // When adopting aggressive denoiser, the adj_val for each pixel
136     // could be at most 8 (this is current max adjustment of the map).
137     // In SSE code, we calculate the sum of adj_val for
138     // the columns, so the sum could be upto 128(16 rows). However,
139     // the range of the value is -128 ~ 127 in SSE code, that's why
140     // we do this change in C code.
141     // We don't do this for UV denoiser, since there are only 8 rows,
142     // and max adjustments <= 8, so the sum of the columns will not
143     // exceed 64.
144     if (col_sum[c] >= 128) {
145       col_sum[c] = 127;
146     }
147     sum_diff += col_sum[c];
148   }
149 
150   sum_diff_thresh = SUM_DIFF_THRESHOLD;
151   if (increase_denoising) sum_diff_thresh = SUM_DIFF_THRESHOLD_HIGH;
152   if (abs(sum_diff) > sum_diff_thresh) {
153     // Before returning to copy the block (i.e., apply no denoising), check
154     // if we can still apply some (weaker) temporal filtering to this block,
155     // that would otherwise not be denoised at all. Simplest is to apply
156     // an additional adjustment to running_avg_y to bring it closer to sig.
157     // The adjustment is capped by a maximum delta, and chosen such that
158     // in most cases the resulting sum_diff will be within the
159     // accceptable range given by sum_diff_thresh.
160 
161     // The delta is set by the excess of absolute pixel diff over threshold.
162     int delta = ((abs(sum_diff) - sum_diff_thresh) >> 8) + 1;
163     // Only apply the adjustment for max delta up to 3.
164     if (delta < 4) {
165       sig -= sig_stride * 16;
166       mc_running_avg_y -= mc_avg_y_stride * 16;
167       running_avg_y -= avg_y_stride * 16;
168       for (r = 0; r < 16; ++r) {
169         for (c = 0; c < 16; ++c) {
170           int diff = mc_running_avg_y[c] - sig[c];
171           int adjustment = abs(diff);
172           if (adjustment > delta) adjustment = delta;
173           if (diff > 0) {
174             // Bring denoised signal down.
175             if (running_avg_y[c] - adjustment < 0) {
176               running_avg_y[c] = 0;
177             } else {
178               running_avg_y[c] = running_avg_y[c] - adjustment;
179             }
180             col_sum[c] -= adjustment;
181           } else if (diff < 0) {
182             // Bring denoised signal up.
183             if (running_avg_y[c] + adjustment > 255) {
184               running_avg_y[c] = 255;
185             } else {
186               running_avg_y[c] = running_avg_y[c] + adjustment;
187             }
188             col_sum[c] += adjustment;
189           }
190         }
191         // TODO(marpan): Check here if abs(sum_diff) has gone below the
192         // threshold sum_diff_thresh, and if so, we can exit the row loop.
193         sig += sig_stride;
194         mc_running_avg_y += mc_avg_y_stride;
195         running_avg_y += avg_y_stride;
196       }
197 
198       sum_diff = 0;
199       for (c = 0; c < 16; ++c) {
200         if (col_sum[c] >= 128) {
201           col_sum[c] = 127;
202         }
203         sum_diff += col_sum[c];
204       }
205 
206       if (abs(sum_diff) > sum_diff_thresh) return COPY_BLOCK;
207     } else {
208       return COPY_BLOCK;
209     }
210   }
211 
212   vp8_copy_mem16x16(running_avg_y_start, avg_y_stride, sig_start, sig_stride);
213   return FILTER_BLOCK;
214 }
215 
vp8_denoiser_filter_uv_c(unsigned char * mc_running_avg,int mc_avg_stride,unsigned char * running_avg,int avg_stride,unsigned char * sig,int sig_stride,unsigned int motion_magnitude,int increase_denoising)216 int vp8_denoiser_filter_uv_c(unsigned char *mc_running_avg, int mc_avg_stride,
217                              unsigned char *running_avg, int avg_stride,
218                              unsigned char *sig, int sig_stride,
219                              unsigned int motion_magnitude,
220                              int increase_denoising) {
221   unsigned char *running_avg_start = running_avg;
222   unsigned char *sig_start = sig;
223   int sum_diff_thresh;
224   int r, c;
225   int sum_diff = 0;
226   int sum_block = 0;
227   int adj_val[3] = { 3, 4, 6 };
228   int shift_inc1 = 0;
229   int shift_inc2 = 1;
230   /* If motion_magnitude is small, making the denoiser more aggressive by
231    * increasing the adjustment for each level. Add another increment for
232    * blocks that are labeled for increase denoising. */
233   if (motion_magnitude <= MOTION_MAGNITUDE_THRESHOLD_UV) {
234     if (increase_denoising) {
235       shift_inc1 = 1;
236       shift_inc2 = 2;
237     }
238     adj_val[0] += shift_inc2;
239     adj_val[1] += shift_inc2;
240     adj_val[2] += shift_inc2;
241   }
242 
243   // Avoid denoising color signal if its close to average level.
244   for (r = 0; r < 8; ++r) {
245     for (c = 0; c < 8; ++c) {
246       sum_block += sig[c];
247     }
248     sig += sig_stride;
249   }
250   if (abs(sum_block - (128 * 8 * 8)) < SUM_DIFF_FROM_AVG_THRESH_UV) {
251     return COPY_BLOCK;
252   }
253 
254   sig -= sig_stride * 8;
255   for (r = 0; r < 8; ++r) {
256     for (c = 0; c < 8; ++c) {
257       int diff = 0;
258       int adjustment = 0;
259       int absdiff = 0;
260 
261       diff = mc_running_avg[c] - sig[c];
262       absdiff = abs(diff);
263 
264       // When |diff| <= |3 + shift_inc1|, use pixel value from
265       // last denoised raw.
266       if (absdiff <= 3 + shift_inc1) {
267         running_avg[c] = mc_running_avg[c];
268         sum_diff += diff;
269       } else {
270         if (absdiff >= 4 && absdiff <= 7) {
271           adjustment = adj_val[0];
272         } else if (absdiff >= 8 && absdiff <= 15) {
273           adjustment = adj_val[1];
274         } else {
275           adjustment = adj_val[2];
276         }
277         if (diff > 0) {
278           if ((sig[c] + adjustment) > 255) {
279             running_avg[c] = 255;
280           } else {
281             running_avg[c] = sig[c] + adjustment;
282           }
283           sum_diff += adjustment;
284         } else {
285           if ((sig[c] - adjustment) < 0) {
286             running_avg[c] = 0;
287           } else {
288             running_avg[c] = sig[c] - adjustment;
289           }
290           sum_diff -= adjustment;
291         }
292       }
293     }
294     /* Update pointers for next iteration. */
295     sig += sig_stride;
296     mc_running_avg += mc_avg_stride;
297     running_avg += avg_stride;
298   }
299 
300   sum_diff_thresh = SUM_DIFF_THRESHOLD_UV;
301   if (increase_denoising) sum_diff_thresh = SUM_DIFF_THRESHOLD_HIGH_UV;
302   if (abs(sum_diff) > sum_diff_thresh) {
303     // Before returning to copy the block (i.e., apply no denoising), check
304     // if we can still apply some (weaker) temporal filtering to this block,
305     // that would otherwise not be denoised at all. Simplest is to apply
306     // an additional adjustment to running_avg_y to bring it closer to sig.
307     // The adjustment is capped by a maximum delta, and chosen such that
308     // in most cases the resulting sum_diff will be within the
309     // accceptable range given by sum_diff_thresh.
310 
311     // The delta is set by the excess of absolute pixel diff over threshold.
312     int delta = ((abs(sum_diff) - sum_diff_thresh) >> 8) + 1;
313     // Only apply the adjustment for max delta up to 3.
314     if (delta < 4) {
315       sig -= sig_stride * 8;
316       mc_running_avg -= mc_avg_stride * 8;
317       running_avg -= avg_stride * 8;
318       for (r = 0; r < 8; ++r) {
319         for (c = 0; c < 8; ++c) {
320           int diff = mc_running_avg[c] - sig[c];
321           int adjustment = abs(diff);
322           if (adjustment > delta) adjustment = delta;
323           if (diff > 0) {
324             // Bring denoised signal down.
325             if (running_avg[c] - adjustment < 0) {
326               running_avg[c] = 0;
327             } else {
328               running_avg[c] = running_avg[c] - adjustment;
329             }
330             sum_diff -= adjustment;
331           } else if (diff < 0) {
332             // Bring denoised signal up.
333             if (running_avg[c] + adjustment > 255) {
334               running_avg[c] = 255;
335             } else {
336               running_avg[c] = running_avg[c] + adjustment;
337             }
338             sum_diff += adjustment;
339           }
340         }
341         // TODO(marpan): Check here if abs(sum_diff) has gone below the
342         // threshold sum_diff_thresh, and if so, we can exit the row loop.
343         sig += sig_stride;
344         mc_running_avg += mc_avg_stride;
345         running_avg += avg_stride;
346       }
347       if (abs(sum_diff) > sum_diff_thresh) return COPY_BLOCK;
348     } else {
349       return COPY_BLOCK;
350     }
351   }
352 
353   vp8_copy_mem8x8(running_avg_start, avg_stride, sig_start, sig_stride);
354   return FILTER_BLOCK;
355 }
356 
vp8_denoiser_set_parameters(VP8_DENOISER * denoiser,int mode)357 void vp8_denoiser_set_parameters(VP8_DENOISER *denoiser, int mode) {
358   assert(mode > 0);  // Denoiser is allocated only if mode > 0.
359   if (mode == 1) {
360     denoiser->denoiser_mode = kDenoiserOnYOnly;
361   } else if (mode == 2) {
362     denoiser->denoiser_mode = kDenoiserOnYUV;
363   } else if (mode == 3) {
364     denoiser->denoiser_mode = kDenoiserOnYUVAggressive;
365   } else {
366     denoiser->denoiser_mode = kDenoiserOnYUV;
367   }
368   if (denoiser->denoiser_mode != kDenoiserOnYUVAggressive) {
369     denoiser->denoise_pars.scale_sse_thresh = 1;
370     denoiser->denoise_pars.scale_motion_thresh = 8;
371     denoiser->denoise_pars.scale_increase_filter = 0;
372     denoiser->denoise_pars.denoise_mv_bias = 95;
373     denoiser->denoise_pars.pickmode_mv_bias = 100;
374     denoiser->denoise_pars.qp_thresh = 0;
375     denoiser->denoise_pars.consec_zerolast = UINT_MAX;
376     denoiser->denoise_pars.spatial_blur = 0;
377   } else {
378     denoiser->denoise_pars.scale_sse_thresh = 2;
379     denoiser->denoise_pars.scale_motion_thresh = 16;
380     denoiser->denoise_pars.scale_increase_filter = 1;
381     denoiser->denoise_pars.denoise_mv_bias = 60;
382     denoiser->denoise_pars.pickmode_mv_bias = 75;
383     denoiser->denoise_pars.qp_thresh = 80;
384     denoiser->denoise_pars.consec_zerolast = 15;
385     denoiser->denoise_pars.spatial_blur = 0;
386   }
387 }
388 
vp8_denoiser_allocate(VP8_DENOISER * denoiser,int width,int height,int num_mb_rows,int num_mb_cols,int mode)389 int vp8_denoiser_allocate(VP8_DENOISER *denoiser, int width, int height,
390                           int num_mb_rows, int num_mb_cols, int mode) {
391   int i;
392   assert(denoiser);
393   denoiser->num_mb_cols = num_mb_cols;
394 
395   for (i = 0; i < MAX_REF_FRAMES; ++i) {
396     denoiser->yv12_running_avg[i].flags = 0;
397 
398     if (vp8_yv12_alloc_frame_buffer(&(denoiser->yv12_running_avg[i]), width,
399                                     height, VP8BORDERINPIXELS) < 0) {
400       vp8_denoiser_free(denoiser);
401       return 1;
402     }
403     memset(denoiser->yv12_running_avg[i].buffer_alloc, 0,
404            denoiser->yv12_running_avg[i].frame_size);
405   }
406   denoiser->yv12_mc_running_avg.flags = 0;
407 
408   if (vp8_yv12_alloc_frame_buffer(&(denoiser->yv12_mc_running_avg), width,
409                                   height, VP8BORDERINPIXELS) < 0) {
410     vp8_denoiser_free(denoiser);
411     return 1;
412   }
413 
414   memset(denoiser->yv12_mc_running_avg.buffer_alloc, 0,
415          denoiser->yv12_mc_running_avg.frame_size);
416 
417   if (vp8_yv12_alloc_frame_buffer(&denoiser->yv12_last_source, width, height,
418                                   VP8BORDERINPIXELS) < 0) {
419     vp8_denoiser_free(denoiser);
420     return 1;
421   }
422   memset(denoiser->yv12_last_source.buffer_alloc, 0,
423          denoiser->yv12_last_source.frame_size);
424 
425   denoiser->denoise_state = vpx_calloc((num_mb_rows * num_mb_cols), 1);
426   if (!denoiser->denoise_state) {
427     vp8_denoiser_free(denoiser);
428     return 1;
429   }
430   memset(denoiser->denoise_state, 0, (num_mb_rows * num_mb_cols));
431   vp8_denoiser_set_parameters(denoiser, mode);
432   denoiser->nmse_source_diff = 0;
433   denoiser->nmse_source_diff_count = 0;
434   denoiser->qp_avg = 0;
435   // QP threshold below which we can go up to aggressive mode.
436   denoiser->qp_threshold_up = 80;
437   // QP threshold above which we can go back down to normal mode.
438   // For now keep this second threshold high, so not used currently.
439   denoiser->qp_threshold_down = 128;
440   // Bitrate thresholds and noise metric (nmse) thresholds for switching to
441   // aggressive mode.
442   // TODO(marpan): Adjust thresholds, including effect on resolution.
443   denoiser->bitrate_threshold = 400000;  // (bits/sec).
444   denoiser->threshold_aggressive_mode = 80;
445   if (width * height > 1280 * 720) {
446     denoiser->bitrate_threshold = 3000000;
447     denoiser->threshold_aggressive_mode = 200;
448   } else if (width * height > 960 * 540) {
449     denoiser->bitrate_threshold = 1200000;
450     denoiser->threshold_aggressive_mode = 120;
451   } else if (width * height > 640 * 480) {
452     denoiser->bitrate_threshold = 600000;
453     denoiser->threshold_aggressive_mode = 100;
454   }
455   return 0;
456 }
457 
vp8_denoiser_free(VP8_DENOISER * denoiser)458 void vp8_denoiser_free(VP8_DENOISER *denoiser) {
459   int i;
460   assert(denoiser);
461 
462   for (i = 0; i < MAX_REF_FRAMES; ++i) {
463     vp8_yv12_de_alloc_frame_buffer(&denoiser->yv12_running_avg[i]);
464   }
465   vp8_yv12_de_alloc_frame_buffer(&denoiser->yv12_mc_running_avg);
466   vp8_yv12_de_alloc_frame_buffer(&denoiser->yv12_last_source);
467   vpx_free(denoiser->denoise_state);
468 }
469 
vp8_denoiser_denoise_mb(VP8_DENOISER * denoiser,MACROBLOCK * x,unsigned int best_sse,unsigned int zero_mv_sse,int recon_yoffset,int recon_uvoffset,loop_filter_info_n * lfi_n,int mb_row,int mb_col,int block_index,int consec_zero_last)470 void vp8_denoiser_denoise_mb(VP8_DENOISER *denoiser, MACROBLOCK *x,
471                              unsigned int best_sse, unsigned int zero_mv_sse,
472                              int recon_yoffset, int recon_uvoffset,
473                              loop_filter_info_n *lfi_n, int mb_row, int mb_col,
474                              int block_index, int consec_zero_last)
475 
476 {
477   int mv_row;
478   int mv_col;
479   unsigned int motion_threshold;
480   unsigned int motion_magnitude2;
481   unsigned int sse_thresh;
482   int sse_diff_thresh = 0;
483   // Spatial loop filter: only applied selectively based on
484   // temporal filter state of block relative to top/left neighbors.
485   int apply_spatial_loop_filter = 1;
486   MV_REFERENCE_FRAME frame = x->best_reference_frame;
487   MV_REFERENCE_FRAME zero_frame = x->best_zeromv_reference_frame;
488 
489   enum vp8_denoiser_decision decision = FILTER_BLOCK;
490   enum vp8_denoiser_decision decision_u = COPY_BLOCK;
491   enum vp8_denoiser_decision decision_v = COPY_BLOCK;
492 
493   if (zero_frame) {
494     YV12_BUFFER_CONFIG *src = &denoiser->yv12_running_avg[frame];
495     YV12_BUFFER_CONFIG *dst = &denoiser->yv12_mc_running_avg;
496     YV12_BUFFER_CONFIG saved_pre, saved_dst;
497     MB_MODE_INFO saved_mbmi;
498     MACROBLOCKD *filter_xd = &x->e_mbd;
499     MB_MODE_INFO *mbmi = &filter_xd->mode_info_context->mbmi;
500     int sse_diff = 0;
501     // Bias on zero motion vector sse.
502     const int zero_bias = denoiser->denoise_pars.denoise_mv_bias;
503     zero_mv_sse = (unsigned int)((int64_t)zero_mv_sse * zero_bias / 100);
504     sse_diff = (int)zero_mv_sse - (int)best_sse;
505 
506     saved_mbmi = *mbmi;
507 
508     /* Use the best MV for the compensation. */
509     mbmi->ref_frame = x->best_reference_frame;
510     mbmi->mode = x->best_sse_inter_mode;
511     mbmi->mv = x->best_sse_mv;
512     mbmi->need_to_clamp_mvs = x->need_to_clamp_best_mvs;
513     mv_col = x->best_sse_mv.as_mv.col;
514     mv_row = x->best_sse_mv.as_mv.row;
515     // Bias to zero_mv if small amount of motion.
516     // Note sse_diff_thresh is intialized to zero, so this ensures
517     // we will always choose zero_mv for denoising if
518     // zero_mv_see <= best_sse (i.e., sse_diff <= 0).
519     if ((unsigned int)(mv_row * mv_row + mv_col * mv_col) <=
520         NOISE_MOTION_THRESHOLD) {
521       sse_diff_thresh = (int)SSE_DIFF_THRESHOLD;
522     }
523 
524     if (frame == INTRA_FRAME || sse_diff <= sse_diff_thresh) {
525       /*
526        * Handle intra blocks as referring to last frame with zero motion
527        * and let the absolute pixel difference affect the filter factor.
528        * Also consider small amount of motion as being random walk due
529        * to noise, if it doesn't mean that we get a much bigger error.
530        * Note that any changes to the mode info only affects the
531        * denoising.
532        */
533       x->denoise_zeromv = 1;
534       mbmi->ref_frame = x->best_zeromv_reference_frame;
535 
536       src = &denoiser->yv12_running_avg[zero_frame];
537 
538       mbmi->mode = ZEROMV;
539       mbmi->mv.as_int = 0;
540       x->best_sse_inter_mode = ZEROMV;
541       x->best_sse_mv.as_int = 0;
542       best_sse = zero_mv_sse;
543     }
544 
545     mv_row = x->best_sse_mv.as_mv.row;
546     mv_col = x->best_sse_mv.as_mv.col;
547     motion_magnitude2 = mv_row * mv_row + mv_col * mv_col;
548     motion_threshold =
549         denoiser->denoise_pars.scale_motion_thresh * NOISE_MOTION_THRESHOLD;
550 
551     if (motion_magnitude2 <
552         denoiser->denoise_pars.scale_increase_filter * NOISE_MOTION_THRESHOLD) {
553       x->increase_denoising = 1;
554     }
555 
556     sse_thresh = denoiser->denoise_pars.scale_sse_thresh * SSE_THRESHOLD;
557     if (x->increase_denoising) {
558       sse_thresh = denoiser->denoise_pars.scale_sse_thresh * SSE_THRESHOLD_HIGH;
559     }
560 
561     if (best_sse > sse_thresh || motion_magnitude2 > motion_threshold) {
562       decision = COPY_BLOCK;
563     }
564 
565     // If block is considered skin, don't denoise if the block
566     // (1) is selected as non-zero motion for current frame, or
567     // (2) has not been selected as ZERO_LAST mode at least x past frames
568     // in a row.
569     // TODO(marpan): Parameter "x" should be varied with framerate.
570     // In particualar, should be reduced for layers (base layer/LAST).
571     if (x->is_skin && (consec_zero_last < 2 || motion_magnitude2 > 0)) {
572       decision = COPY_BLOCK;
573     }
574 
575     if (decision == FILTER_BLOCK) {
576       saved_pre = filter_xd->pre;
577       saved_dst = filter_xd->dst;
578 
579       /* Compensate the running average. */
580       filter_xd->pre.y_buffer = src->y_buffer + recon_yoffset;
581       filter_xd->pre.u_buffer = src->u_buffer + recon_uvoffset;
582       filter_xd->pre.v_buffer = src->v_buffer + recon_uvoffset;
583       /* Write the compensated running average to the destination buffer. */
584       filter_xd->dst.y_buffer = dst->y_buffer + recon_yoffset;
585       filter_xd->dst.u_buffer = dst->u_buffer + recon_uvoffset;
586       filter_xd->dst.v_buffer = dst->v_buffer + recon_uvoffset;
587 
588       if (!x->skip) {
589         vp8_build_inter_predictors_mb(filter_xd);
590       } else {
591         vp8_build_inter16x16_predictors_mb(
592             filter_xd, filter_xd->dst.y_buffer, filter_xd->dst.u_buffer,
593             filter_xd->dst.v_buffer, filter_xd->dst.y_stride,
594             filter_xd->dst.uv_stride);
595       }
596       filter_xd->pre = saved_pre;
597       filter_xd->dst = saved_dst;
598       *mbmi = saved_mbmi;
599     }
600   } else {
601     // zero_frame should always be 1 for real-time mode, as the
602     // ZEROMV mode is always checked, so we should never go into this branch.
603     // If case ZEROMV is not checked, then we will force no denoise (COPY).
604     decision = COPY_BLOCK;
605   }
606 
607   if (decision == FILTER_BLOCK) {
608     unsigned char *mc_running_avg_y =
609         denoiser->yv12_mc_running_avg.y_buffer + recon_yoffset;
610     int mc_avg_y_stride = denoiser->yv12_mc_running_avg.y_stride;
611     unsigned char *running_avg_y =
612         denoiser->yv12_running_avg[INTRA_FRAME].y_buffer + recon_yoffset;
613     int avg_y_stride = denoiser->yv12_running_avg[INTRA_FRAME].y_stride;
614 
615     /* Filter. */
616     decision = vp8_denoiser_filter(mc_running_avg_y, mc_avg_y_stride,
617                                    running_avg_y, avg_y_stride, x->thismb, 16,
618                                    motion_magnitude2, x->increase_denoising);
619     denoiser->denoise_state[block_index] =
620         motion_magnitude2 > 0 ? kFilterNonZeroMV : kFilterZeroMV;
621     // Only denoise UV for zero motion, and if y channel was denoised.
622     if (denoiser->denoiser_mode != kDenoiserOnYOnly && motion_magnitude2 == 0 &&
623         decision == FILTER_BLOCK) {
624       unsigned char *mc_running_avg_u =
625           denoiser->yv12_mc_running_avg.u_buffer + recon_uvoffset;
626       unsigned char *running_avg_u =
627           denoiser->yv12_running_avg[INTRA_FRAME].u_buffer + recon_uvoffset;
628       unsigned char *mc_running_avg_v =
629           denoiser->yv12_mc_running_avg.v_buffer + recon_uvoffset;
630       unsigned char *running_avg_v =
631           denoiser->yv12_running_avg[INTRA_FRAME].v_buffer + recon_uvoffset;
632       int mc_avg_uv_stride = denoiser->yv12_mc_running_avg.uv_stride;
633       int avg_uv_stride = denoiser->yv12_running_avg[INTRA_FRAME].uv_stride;
634       int signal_stride = x->block[16].src_stride;
635       decision_u = vp8_denoiser_filter_uv(
636           mc_running_avg_u, mc_avg_uv_stride, running_avg_u, avg_uv_stride,
637           x->block[16].src + *x->block[16].base_src, signal_stride,
638           motion_magnitude2, 0);
639       decision_v = vp8_denoiser_filter_uv(
640           mc_running_avg_v, mc_avg_uv_stride, running_avg_v, avg_uv_stride,
641           x->block[20].src + *x->block[20].base_src, signal_stride,
642           motion_magnitude2, 0);
643     }
644   }
645   if (decision == COPY_BLOCK) {
646     /* No filtering of this block; it differs too much from the predictor,
647      * or the motion vector magnitude is considered too big.
648      */
649     x->denoise_zeromv = 0;
650     vp8_copy_mem16x16(
651         x->thismb, 16,
652         denoiser->yv12_running_avg[INTRA_FRAME].y_buffer + recon_yoffset,
653         denoiser->yv12_running_avg[INTRA_FRAME].y_stride);
654     denoiser->denoise_state[block_index] = kNoFilter;
655   }
656   if (denoiser->denoiser_mode != kDenoiserOnYOnly) {
657     if (decision_u == COPY_BLOCK) {
658       vp8_copy_mem8x8(
659           x->block[16].src + *x->block[16].base_src, x->block[16].src_stride,
660           denoiser->yv12_running_avg[INTRA_FRAME].u_buffer + recon_uvoffset,
661           denoiser->yv12_running_avg[INTRA_FRAME].uv_stride);
662     }
663     if (decision_v == COPY_BLOCK) {
664       vp8_copy_mem8x8(
665           x->block[20].src + *x->block[20].base_src, x->block[16].src_stride,
666           denoiser->yv12_running_avg[INTRA_FRAME].v_buffer + recon_uvoffset,
667           denoiser->yv12_running_avg[INTRA_FRAME].uv_stride);
668     }
669   }
670   // Option to selectively deblock the denoised signal, for y channel only.
671   if (apply_spatial_loop_filter) {
672     loop_filter_info lfi;
673     int apply_filter_col = 0;
674     int apply_filter_row = 0;
675     int apply_filter = 0;
676     int y_stride = denoiser->yv12_running_avg[INTRA_FRAME].y_stride;
677     int uv_stride = denoiser->yv12_running_avg[INTRA_FRAME].uv_stride;
678 
679     // Fix filter level to some nominal value for now.
680     int filter_level = 48;
681 
682     int hev_index = lfi_n->hev_thr_lut[INTER_FRAME][filter_level];
683     lfi.mblim = lfi_n->mblim[filter_level];
684     lfi.blim = lfi_n->blim[filter_level];
685     lfi.lim = lfi_n->lim[filter_level];
686     lfi.hev_thr = lfi_n->hev_thr[hev_index];
687 
688     // Apply filter if there is a difference in the denoiser filter state
689     // between the current and left/top block, or if non-zero motion vector
690     // is used for the motion-compensated filtering.
691     if (mb_col > 0) {
692       apply_filter_col =
693           !((denoiser->denoise_state[block_index] ==
694              denoiser->denoise_state[block_index - 1]) &&
695             denoiser->denoise_state[block_index] != kFilterNonZeroMV);
696       if (apply_filter_col) {
697         // Filter left vertical edge.
698         apply_filter = 1;
699         vp8_loop_filter_mbv(
700             denoiser->yv12_running_avg[INTRA_FRAME].y_buffer + recon_yoffset,
701             NULL, NULL, y_stride, uv_stride, &lfi);
702       }
703     }
704     if (mb_row > 0) {
705       apply_filter_row =
706           !((denoiser->denoise_state[block_index] ==
707              denoiser->denoise_state[block_index - denoiser->num_mb_cols]) &&
708             denoiser->denoise_state[block_index] != kFilterNonZeroMV);
709       if (apply_filter_row) {
710         // Filter top horizontal edge.
711         apply_filter = 1;
712         vp8_loop_filter_mbh(
713             denoiser->yv12_running_avg[INTRA_FRAME].y_buffer + recon_yoffset,
714             NULL, NULL, y_stride, uv_stride, &lfi);
715       }
716     }
717     if (apply_filter) {
718       // Update the signal block |x|. Pixel changes are only to top and/or
719       // left boundary pixels: can we avoid full block copy here.
720       vp8_copy_mem16x16(
721           denoiser->yv12_running_avg[INTRA_FRAME].y_buffer + recon_yoffset,
722           y_stride, x->thismb, 16);
723     }
724   }
725 }
726