]> granicus.if.org Git - libvpx/blob - vp8/encoder/denoising.c
Merge "[spatial svc]Add a few different encode frame tests."
[libvpx] / vp8 / encoder / denoising.c
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 * 60;
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
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 {
63     unsigned char *running_avg_y_start = running_avg_y;
64     unsigned char *sig_start = sig;
65     int sum_diff_thresh;
66     int r, c;
67     int sum_diff = 0;
68     int adj_val[3] = {3, 4, 6};
69     int shift_inc1 = 0;
70     int shift_inc2 = 1;
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     {
76       if (increase_denoising) {
77         shift_inc1 = 1;
78         shift_inc2 = 2;
79       }
80       adj_val[0] += shift_inc2;
81       adj_val[1] += shift_inc2;
82       adj_val[2] += shift_inc2;
83     }
84
85     for (r = 0; r < 16; ++r)
86     {
87         for (c = 0; c < 16; ++c)
88         {
89             int diff = 0;
90             int adjustment = 0;
91             int absdiff = 0;
92
93             diff = mc_running_avg_y[c] - sig[c];
94             absdiff = abs(diff);
95
96             // When |diff| <= |3 + shift_inc1|, use pixel value from
97             // last denoised raw.
98             if (absdiff <= 3 + shift_inc1)
99             {
100                 running_avg_y[c] = mc_running_avg_y[c];
101                 sum_diff += diff;
102             }
103             else
104             {
105                 if (absdiff >= 4 && absdiff <= 7)
106                     adjustment = adj_val[0];
107                 else if (absdiff >= 8 && absdiff <= 15)
108                     adjustment = adj_val[1];
109                 else
110                     adjustment = adj_val[2];
111
112                 if (diff > 0)
113                 {
114                     if ((sig[c] + adjustment) > 255)
115                         running_avg_y[c] = 255;
116                     else
117                         running_avg_y[c] = sig[c] + adjustment;
118
119                     sum_diff += adjustment;
120                 }
121                 else
122                 {
123                     if ((sig[c] - adjustment) < 0)
124                         running_avg_y[c] = 0;
125                     else
126                         running_avg_y[c] = sig[c] - adjustment;
127
128                     sum_diff -= adjustment;
129                 }
130             }
131         }
132
133         /* Update pointers for next iteration. */
134         sig += sig_stride;
135         mc_running_avg_y += mc_avg_y_stride;
136         running_avg_y += avg_y_stride;
137     }
138
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.
149
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.
153       if (delta < 4) {
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)
162               adjustment = delta;
163             if (diff > 0) {
164               // Bring denoised signal down.
165               if (running_avg_y[c] - adjustment < 0)
166                 running_avg_y[c] = 0;
167               else
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;
174               else
175                 running_avg_y[c] = running_avg_y[c] + adjustment;
176               sum_diff += adjustment;
177             }
178           }
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.
181           sig += sig_stride;
182           mc_running_avg_y += mc_avg_y_stride;
183           running_avg_y += avg_y_stride;
184         }
185         if (abs(sum_diff) > sum_diff_thresh)
186           return COPY_BLOCK;
187       } else {
188         return COPY_BLOCK;
189       }
190     }
191
192     vp8_copy_mem16x16(running_avg_y_start, avg_y_stride, sig_start, sig_stride);
193     return FILTER_BLOCK;
194 }
195
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,
199                              int avg_uv_stride,
200                              unsigned char *sig,
201                              int sig_stride,
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;
206     int sum_diff_thresh;
207     int r, c;
208     int sum_diff = 0;
209     int sum_block = 0;
210     int adj_val[3] = {3, 4, 6};
211     int shift_inc1 = 0;
212     int shift_inc2 = 1;
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) {
218         shift_inc1 = 1;
219         shift_inc2 = 2;
220       }
221       adj_val[0] += shift_inc2;
222       adj_val[1] += shift_inc2;
223       adj_val[2] += shift_inc2;
224     }
225
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) {
229         sum_block += sig[c];
230       }
231       sig += sig_stride;
232     }
233     if (abs(sum_block - (128 * 8 * 8)) < SUM_DIFF_FROM_AVG_THRESH_UV) {
234       return COPY_BLOCK;
235     }
236
237     sig -= sig_stride * 8;
238     for (r = 0; r < 8; ++r) {
239       for (c = 0; c < 8; ++c) {
240         int diff = 0;
241         int adjustment = 0;
242         int absdiff = 0;
243
244         diff = mc_running_avg_uv[c] - sig[c];
245         absdiff = abs(diff);
246
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];
251           sum_diff += diff;
252         } else {
253           if (absdiff >= 4 && absdiff <= 7)
254             adjustment = adj_val[0];
255           else if (absdiff >= 8 && absdiff <= 15)
256             adjustment = adj_val[1];
257           else
258             adjustment = adj_val[2];
259           if (diff > 0) {
260             if ((sig[c] + adjustment) > 255)
261               running_avg_uv[c] = 255;
262             else
263               running_avg_uv[c] = sig[c] + adjustment;
264             sum_diff += adjustment;
265           } else {
266             if ((sig[c] - adjustment) < 0)
267               running_avg_uv[c] = 0;
268             else
269               running_avg_uv[c] = sig[c] - adjustment;
270             sum_diff -= adjustment;
271           }
272         }
273       }
274       /* Update pointers for next iteration. */
275       sig += sig_stride;
276       mc_running_avg_uv += mc_avg_uv_stride;
277       running_avg_uv += avg_uv_stride;
278     }
279
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.
290
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.
294       if (delta < 4) {
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)
303               adjustment = delta;
304             if (diff > 0) {
305               // Bring denoised signal down.
306               if (running_avg_uv[c] - adjustment < 0)
307                 running_avg_uv[c] = 0;
308               else
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;
315               else
316                 running_avg_uv[c] = running_avg_uv[c] + adjustment;
317               sum_diff += adjustment;
318             }
319           }
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.
322           sig += sig_stride;
323           mc_running_avg_uv += mc_avg_uv_stride;
324           running_avg_uv += avg_uv_stride;
325         }
326         if (abs(sum_diff) > sum_diff_thresh)
327           return COPY_BLOCK;
328       } else {
329         return COPY_BLOCK;
330       }
331     }
332
333     vp8_copy_mem8x8(running_avg_uv_start, avg_uv_stride, sig_start,
334                     sig_stride);
335     return FILTER_BLOCK;
336 }
337
338 void vp8_denoiser_set_parameters(VP8_DENOISER *denoiser, int mode) {
339   assert(mode > 0);  // Denoiser is allocated only if mode > 0.
340   if (mode == 1) {
341     denoiser->denoiser_mode = kDenoiserOnYOnly;
342   } else if (mode == 2) {
343     denoiser->denoiser_mode = kDenoiserOnYUV;
344   } else if (mode == 3) {
345     denoiser->denoiser_mode = kDenoiserOnYUVAggressive;
346   } else {
347     denoiser->denoiser_mode = kDenoiserOnAdaptive;
348   }
349   if (denoiser->denoiser_mode != kDenoiserOnYUVAggressive) {
350     denoiser->denoise_pars.scale_sse_thresh = 1;
351     denoiser->denoise_pars.scale_motion_thresh = 8;
352     denoiser->denoise_pars.scale_increase_filter = 0;
353     denoiser->denoise_pars.denoise_mv_bias = 95;
354     denoiser->denoise_pars.pickmode_mv_bias = 100;
355     denoiser->denoise_pars.qp_thresh = 0;
356     denoiser->denoise_pars.consec_zerolast = UINT_MAX;
357   } else {
358     denoiser->denoise_pars.scale_sse_thresh = 2;
359     denoiser->denoise_pars.scale_motion_thresh = 16;
360     denoiser->denoise_pars.scale_increase_filter = 1;
361     denoiser->denoise_pars.denoise_mv_bias = 60;
362     denoiser->denoise_pars.pickmode_mv_bias = 60;
363     denoiser->denoise_pars.qp_thresh = 100;
364     denoiser->denoise_pars.consec_zerolast = 10;
365   }
366 }
367
368 int vp8_denoiser_allocate(VP8_DENOISER *denoiser, int width, int height,
369                           int num_mb_rows, int num_mb_cols, int mode)
370 {
371     int i;
372     assert(denoiser);
373     denoiser->num_mb_cols = num_mb_cols;
374
375     for (i = 0; i < MAX_REF_FRAMES; i++)
376     {
377         denoiser->yv12_running_avg[i].flags = 0;
378
379         if (vp8_yv12_alloc_frame_buffer(&(denoiser->yv12_running_avg[i]), width,
380                                         height, VP8BORDERINPIXELS)
381             < 0)
382         {
383             vp8_denoiser_free(denoiser);
384             return 1;
385         }
386         vpx_memset(denoiser->yv12_running_avg[i].buffer_alloc, 0,
387                    denoiser->yv12_running_avg[i].frame_size);
388
389     }
390     denoiser->yv12_mc_running_avg.flags = 0;
391
392     if (vp8_yv12_alloc_frame_buffer(&(denoiser->yv12_mc_running_avg), width,
393                                    height, VP8BORDERINPIXELS) < 0)
394     {
395         vp8_denoiser_free(denoiser);
396         return 1;
397     }
398
399     vpx_memset(denoiser->yv12_mc_running_avg.buffer_alloc, 0,
400                denoiser->yv12_mc_running_avg.frame_size);
401
402     if (vp8_yv12_alloc_frame_buffer(&denoiser->yv12_last_source, width,
403                                     height, VP8BORDERINPIXELS) < 0) {
404       vp8_denoiser_free(denoiser);
405       return 1;
406     }
407     vpx_memset(denoiser->yv12_last_source.buffer_alloc, 0,
408                denoiser->yv12_last_source.frame_size);
409
410     denoiser->denoise_state = vpx_calloc((num_mb_rows * num_mb_cols), 1);
411     vpx_memset(denoiser->denoise_state, 0, (num_mb_rows * num_mb_cols));
412     vp8_denoiser_set_parameters(denoiser, mode);
413     denoiser->nmse_source_diff = 0;
414     denoiser->nmse_source_diff_count = 0;
415     // TODO(marpan): Adjust thresholds, including effect on resolution.
416     denoiser->threshold_aggressive_mode = 40;
417     if (width * height > 640 * 480)
418       denoiser->threshold_aggressive_mode = 180;
419     return 0;
420 }
421
422
423 void vp8_denoiser_free(VP8_DENOISER *denoiser)
424 {
425     int i;
426     assert(denoiser);
427
428     for (i = 0; i < MAX_REF_FRAMES ; i++)
429     {
430         vp8_yv12_de_alloc_frame_buffer(&denoiser->yv12_running_avg[i]);
431     }
432     vp8_yv12_de_alloc_frame_buffer(&denoiser->yv12_mc_running_avg);
433     vpx_free(denoiser->denoise_state);
434 }
435
436
437 void vp8_denoiser_denoise_mb(VP8_DENOISER *denoiser,
438                              MACROBLOCK *x,
439                              unsigned int best_sse,
440                              unsigned int zero_mv_sse,
441                              int recon_yoffset,
442                              int recon_uvoffset,
443                              loop_filter_info_n *lfi_n,
444                              int mb_row,
445                              int mb_col,
446                              int block_index)
447
448 {
449     int mv_row;
450     int mv_col;
451     unsigned int motion_threshold;
452     unsigned int motion_magnitude2;
453     unsigned int sse_thresh;
454     int sse_diff_thresh = 0;
455     // Spatial loop filter: only applied selectively based on
456     // temporal filter state of block relative to top/left neighbors.
457     int apply_spatial_loop_filter = 1;
458     MV_REFERENCE_FRAME frame = x->best_reference_frame;
459     MV_REFERENCE_FRAME zero_frame = x->best_zeromv_reference_frame;
460
461     enum vp8_denoiser_decision decision = FILTER_BLOCK;
462     enum vp8_denoiser_decision decision_u = COPY_BLOCK;
463     enum vp8_denoiser_decision decision_v = COPY_BLOCK;
464
465     if (zero_frame)
466     {
467         YV12_BUFFER_CONFIG *src = &denoiser->yv12_running_avg[frame];
468         YV12_BUFFER_CONFIG *dst = &denoiser->yv12_mc_running_avg;
469         YV12_BUFFER_CONFIG saved_pre,saved_dst;
470         MB_MODE_INFO saved_mbmi;
471         MACROBLOCKD *filter_xd = &x->e_mbd;
472         MB_MODE_INFO *mbmi = &filter_xd->mode_info_context->mbmi;
473         int sse_diff = 0;
474         // Bias on zero motion vector sse.
475         const int zero_bias = denoiser->denoise_pars.denoise_mv_bias;
476         zero_mv_sse = (unsigned int)((int64_t)zero_mv_sse * zero_bias / 100);
477         sse_diff = zero_mv_sse - best_sse;
478
479         saved_mbmi = *mbmi;
480
481         /* Use the best MV for the compensation. */
482         mbmi->ref_frame = x->best_reference_frame;
483         mbmi->mode = x->best_sse_inter_mode;
484         mbmi->mv = x->best_sse_mv;
485         mbmi->need_to_clamp_mvs = x->need_to_clamp_best_mvs;
486         mv_col = x->best_sse_mv.as_mv.col;
487         mv_row = x->best_sse_mv.as_mv.row;
488         // Bias to zero_mv if small amount of motion.
489         // Note sse_diff_thresh is intialized to zero, so this ensures
490         // we will always choose zero_mv for denoising if
491         // zero_mv_see <= best_sse (i.e., sse_diff <= 0).
492         if ((unsigned int)(mv_row * mv_row + mv_col * mv_col)
493             <= NOISE_MOTION_THRESHOLD)
494             sse_diff_thresh = (int)SSE_DIFF_THRESHOLD;
495
496         if (frame == INTRA_FRAME ||
497             sse_diff <= sse_diff_thresh)
498         {
499             /*
500              * Handle intra blocks as referring to last frame with zero motion
501              * and let the absolute pixel difference affect the filter factor.
502              * Also consider small amount of motion as being random walk due
503              * to noise, if it doesn't mean that we get a much bigger error.
504              * Note that any changes to the mode info only affects the
505              * denoising.
506              */
507             mbmi->ref_frame =
508                     x->best_zeromv_reference_frame;
509
510             src = &denoiser->yv12_running_avg[zero_frame];
511
512             mbmi->mode = ZEROMV;
513             mbmi->mv.as_int = 0;
514             x->best_sse_inter_mode = ZEROMV;
515             x->best_sse_mv.as_int = 0;
516             best_sse = zero_mv_sse;
517         }
518
519         saved_pre = filter_xd->pre;
520         saved_dst = filter_xd->dst;
521
522         /* Compensate the running average. */
523         filter_xd->pre.y_buffer = src->y_buffer + recon_yoffset;
524         filter_xd->pre.u_buffer = src->u_buffer + recon_uvoffset;
525         filter_xd->pre.v_buffer = src->v_buffer + recon_uvoffset;
526         /* Write the compensated running average to the destination buffer. */
527         filter_xd->dst.y_buffer = dst->y_buffer + recon_yoffset;
528         filter_xd->dst.u_buffer = dst->u_buffer + recon_uvoffset;
529         filter_xd->dst.v_buffer = dst->v_buffer + recon_uvoffset;
530
531         if (!x->skip)
532         {
533             vp8_build_inter_predictors_mb(filter_xd);
534         }
535         else
536         {
537             vp8_build_inter16x16_predictors_mb(filter_xd,
538                                                filter_xd->dst.y_buffer,
539                                                filter_xd->dst.u_buffer,
540                                                filter_xd->dst.v_buffer,
541                                                filter_xd->dst.y_stride,
542                                                filter_xd->dst.uv_stride);
543         }
544         filter_xd->pre = saved_pre;
545         filter_xd->dst = saved_dst;
546         *mbmi = saved_mbmi;
547
548     }
549
550     mv_row = x->best_sse_mv.as_mv.row;
551     mv_col = x->best_sse_mv.as_mv.col;
552     motion_magnitude2 = mv_row * mv_row + mv_col * mv_col;
553     motion_threshold = denoiser->denoise_pars.scale_motion_thresh *
554         NOISE_MOTION_THRESHOLD;
555
556     if (motion_magnitude2 <
557         denoiser->denoise_pars.scale_increase_filter * NOISE_MOTION_THRESHOLD)
558       x->increase_denoising = 1;
559
560     sse_thresh = denoiser->denoise_pars.scale_sse_thresh * SSE_THRESHOLD;
561     if (x->increase_denoising)
562       sse_thresh = denoiser->denoise_pars.scale_sse_thresh * SSE_THRESHOLD_HIGH;
563
564     if (best_sse > sse_thresh || motion_magnitude2 > motion_threshold)
565       decision = COPY_BLOCK;
566
567     if (decision == FILTER_BLOCK)
568     {
569         unsigned char *mc_running_avg_y =
570             denoiser->yv12_mc_running_avg.y_buffer + recon_yoffset;
571         int mc_avg_y_stride = denoiser->yv12_mc_running_avg.y_stride;
572         unsigned char *running_avg_y =
573             denoiser->yv12_running_avg[INTRA_FRAME].y_buffer + recon_yoffset;
574         int avg_y_stride = denoiser->yv12_running_avg[INTRA_FRAME].y_stride;
575
576         /* Filter. */
577         decision = vp8_denoiser_filter(mc_running_avg_y, mc_avg_y_stride,
578                                        running_avg_y, avg_y_stride,
579                                        x->thismb, 16, motion_magnitude2,
580                                        x->increase_denoising);
581         denoiser->denoise_state[block_index] = motion_magnitude2 > 0 ?
582             kFilterNonZeroMV : kFilterZeroMV;
583         // Only denoise UV for zero motion, and if y channel was denoised.
584         if (denoiser->denoiser_mode != kDenoiserOnYOnly &&
585             motion_magnitude2 == 0 &&
586             decision == FILTER_BLOCK) {
587           unsigned char *mc_running_avg_u =
588               denoiser->yv12_mc_running_avg.u_buffer + recon_uvoffset;
589           unsigned char *running_avg_u =
590               denoiser->yv12_running_avg[INTRA_FRAME].u_buffer + recon_uvoffset;
591           unsigned char *mc_running_avg_v =
592               denoiser->yv12_mc_running_avg.v_buffer + recon_uvoffset;
593           unsigned char *running_avg_v =
594               denoiser->yv12_running_avg[INTRA_FRAME].v_buffer + recon_uvoffset;
595           int mc_avg_uv_stride = denoiser->yv12_mc_running_avg.uv_stride;
596           int avg_uv_stride = denoiser->yv12_running_avg[INTRA_FRAME].uv_stride;
597           int signal_stride = x->block[16].src_stride;
598           decision_u =
599               vp8_denoiser_filter_uv(mc_running_avg_u, mc_avg_uv_stride,
600                                       running_avg_u, avg_uv_stride,
601                                       x->block[16].src + *x->block[16].base_src,
602                                       signal_stride, motion_magnitude2, 0);
603           decision_v =
604               vp8_denoiser_filter_uv(mc_running_avg_v, mc_avg_uv_stride,
605                                       running_avg_v, avg_uv_stride,
606                                       x->block[20].src + *x->block[20].base_src,
607                                       signal_stride, motion_magnitude2, 0);
608         }
609     }
610     if (decision == COPY_BLOCK)
611     {
612         /* No filtering of this block; it differs too much from the predictor,
613          * or the motion vector magnitude is considered too big.
614          */
615         vp8_copy_mem16x16(
616                 x->thismb, 16,
617                 denoiser->yv12_running_avg[INTRA_FRAME].y_buffer + recon_yoffset,
618                 denoiser->yv12_running_avg[INTRA_FRAME].y_stride);
619         denoiser->denoise_state[block_index] = kNoFilter;
620     }
621     if (denoiser->denoiser_mode != kDenoiserOnYOnly) {
622       if (decision_u == COPY_BLOCK) {
623         vp8_copy_mem8x8(
624             x->block[16].src + *x->block[16].base_src, x->block[16].src_stride,
625             denoiser->yv12_running_avg[INTRA_FRAME].u_buffer + recon_uvoffset,
626             denoiser->yv12_running_avg[INTRA_FRAME].uv_stride);
627       }
628       if (decision_v == COPY_BLOCK) {
629         vp8_copy_mem8x8(
630             x->block[20].src + *x->block[20].base_src, x->block[16].src_stride,
631             denoiser->yv12_running_avg[INTRA_FRAME].v_buffer + recon_uvoffset,
632             denoiser->yv12_running_avg[INTRA_FRAME].uv_stride);
633       }
634     }
635     // Option to selectively deblock the denoised signal, for y channel only.
636     if (apply_spatial_loop_filter) {
637       loop_filter_info lfi;
638       int apply_filter_col = 0;
639       int apply_filter_row = 0;
640       int apply_filter = 0;
641       int y_stride = denoiser->yv12_running_avg[INTRA_FRAME].y_stride;
642       int uv_stride =denoiser->yv12_running_avg[INTRA_FRAME].uv_stride;
643
644       // Fix filter level to some nominal value for now.
645       int filter_level = 32;
646
647       int hev_index = lfi_n->hev_thr_lut[INTER_FRAME][filter_level];
648       lfi.mblim = lfi_n->mblim[filter_level];
649       lfi.blim = lfi_n->blim[filter_level];
650       lfi.lim = lfi_n->lim[filter_level];
651       lfi.hev_thr = lfi_n->hev_thr[hev_index];
652
653       // Apply filter if there is a difference in the denoiser filter state
654       // between the current and left/top block, or if non-zero motion vector
655       // is used for the motion-compensated filtering.
656       if (mb_col > 0) {
657         apply_filter_col = !((denoiser->denoise_state[block_index] ==
658             denoiser->denoise_state[block_index - 1]) &&
659             denoiser->denoise_state[block_index] != kFilterNonZeroMV);
660         if (apply_filter_col) {
661           // Filter left vertical edge.
662           apply_filter = 1;
663           vp8_loop_filter_mbv(
664               denoiser->yv12_running_avg[INTRA_FRAME].y_buffer + recon_yoffset,
665               NULL, NULL, y_stride, uv_stride, &lfi);
666         }
667       }
668       if (mb_row > 0) {
669         apply_filter_row = !((denoiser->denoise_state[block_index] ==
670             denoiser->denoise_state[block_index - denoiser->num_mb_cols]) &&
671             denoiser->denoise_state[block_index] != kFilterNonZeroMV);
672         if (apply_filter_row) {
673           // Filter top horizontal edge.
674           apply_filter = 1;
675           vp8_loop_filter_mbh(
676               denoiser->yv12_running_avg[INTRA_FRAME].y_buffer + recon_yoffset,
677               NULL, NULL, y_stride, uv_stride, &lfi);
678         }
679       }
680       if (apply_filter) {
681         // Update the signal block |x|. Pixel changes are only to top and/or
682         // left boundary pixels: can we avoid full block copy here.
683         vp8_copy_mem16x16(
684             denoiser->yv12_running_avg[INTRA_FRAME].y_buffer + recon_yoffset,
685             y_stride, x->thismb, 16);
686       }
687     }
688 }