Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / libvpx / source / 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 "denoising.h"
12
13 #include "vp8/common/reconinter.h"
14 #include "vpx/vpx_integer.h"
15 #include "vpx_mem/vpx_mem.h"
16 #include "vp8_rtcd.h"
17
18 static const unsigned int NOISE_MOTION_THRESHOLD = 25 * 25;
19 /* SSE_DIFF_THRESHOLD is selected as ~95% confidence assuming
20  * var(noise) ~= 100.
21  */
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;
25
26 /*
27  * The filter function was modified to reduce the computational complexity.
28  * Step 1:
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
33  * where
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.
38  *
39  * Step 2:
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
46  * [-255, -16]           -6                                   -7
47  * [-15, -8]             -4                                   -5
48  * [-7, -4]              -3                                   -4
49  * [-3, 3]               diff                                 diff
50  * [4, 7]                 3                                    4
51  * [8, 15]                4                                    5
52  * [16, 255]              6                                    7
53  */
54
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)
60 {
61     unsigned char *running_avg_y_start = running_avg_y;
62     unsigned char *sig_start = sig;
63     int sum_diff_thresh;
64     int r, c;
65     int sum_diff = 0;
66     int adj_val[3] = {3, 4, 6};
67     int shift_inc1 = 0;
68     int shift_inc2 = 1;
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)
73     {
74       if (increase_denoising) {
75         shift_inc1 = 1;
76         shift_inc2 = 2;
77       }
78       adj_val[0] += shift_inc2;
79       adj_val[1] += shift_inc2;
80       adj_val[2] += shift_inc2;
81     }
82
83     for (r = 0; r < 16; ++r)
84     {
85         for (c = 0; c < 16; ++c)
86         {
87             int diff = 0;
88             int adjustment = 0;
89             int absdiff = 0;
90
91             diff = mc_running_avg_y[c] - sig[c];
92             absdiff = abs(diff);
93
94             // When |diff| <= |3 + shift_inc1|, use pixel value from
95             // last denoised raw.
96             if (absdiff <= 3 + shift_inc1)
97             {
98                 running_avg_y[c] = mc_running_avg_y[c];
99                 sum_diff += diff;
100             }
101             else
102             {
103                 if (absdiff >= 4 && absdiff <= 7)
104                     adjustment = adj_val[0];
105                 else if (absdiff >= 8 && absdiff <= 15)
106                     adjustment = adj_val[1];
107                 else
108                     adjustment = adj_val[2];
109
110                 if (diff > 0)
111                 {
112                     if ((sig[c] + adjustment) > 255)
113                         running_avg_y[c] = 255;
114                     else
115                         running_avg_y[c] = sig[c] + adjustment;
116
117                     sum_diff += adjustment;
118                 }
119                 else
120                 {
121                     if ((sig[c] - adjustment) < 0)
122                         running_avg_y[c] = 0;
123                     else
124                         running_avg_y[c] = sig[c] - adjustment;
125
126                     sum_diff -= adjustment;
127                 }
128             }
129         }
130
131         /* Update pointers for next iteration. */
132         sig += sig_stride;
133         mc_running_avg_y += mc_avg_y_stride;
134         running_avg_y += avg_y_stride;
135     }
136
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.
147
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.
151       if (delta < 4) {
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)
160               adjustment = delta;
161             if (diff > 0) {
162               // Bring denoised signal down.
163               if (running_avg_y[c] - adjustment < 0)
164                 running_avg_y[c] = 0;
165               else
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;
172               else
173                 running_avg_y[c] = running_avg_y[c] + adjustment;
174               sum_diff += adjustment;
175             }
176           }
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.
179           sig += sig_stride;
180           mc_running_avg_y += mc_avg_y_stride;
181           running_avg_y += avg_y_stride;
182         }
183         if (abs(sum_diff) > sum_diff_thresh)
184           return COPY_BLOCK;
185       } else {
186         return COPY_BLOCK;
187       }
188     }
189
190     vp8_copy_mem16x16(running_avg_y_start, avg_y_stride, sig_start, sig_stride);
191     return FILTER_BLOCK;
192 }
193
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,
197                              int avg_uv_stride,
198                              unsigned char *sig,
199                              int sig_stride,
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;
204     int sum_diff_thresh;
205     int r, c;
206     int sum_diff = 0;
207     int sum_block = 0;
208     int adj_val[3] = {3, 4, 6};
209     int shift_inc1 = 0;
210     int shift_inc2 = 1;
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) {
216         shift_inc1 = 1;
217         shift_inc2 = 2;
218       }
219       adj_val[0] += shift_inc2;
220       adj_val[1] += shift_inc2;
221       adj_val[2] += shift_inc2;
222     }
223
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) {
227         sum_block += sig[c];
228       }
229       sig += sig_stride;
230     }
231     if (abs(sum_block - (128 * 8 * 8)) < SUM_DIFF_FROM_AVG_THRESH_UV) {
232       return COPY_BLOCK;
233     }
234
235     sig -= sig_stride * 8;
236     for (r = 0; r < 8; ++r) {
237       for (c = 0; c < 8; ++c) {
238         int diff = 0;
239         int adjustment = 0;
240         int absdiff = 0;
241
242         diff = mc_running_avg_uv[c] - sig[c];
243         absdiff = abs(diff);
244
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];
249           sum_diff += diff;
250         } else {
251           if (absdiff >= 4 && absdiff <= 7)
252             adjustment = adj_val[0];
253           else if (absdiff >= 8 && absdiff <= 15)
254             adjustment = adj_val[1];
255           else
256             adjustment = adj_val[2];
257           if (diff > 0) {
258             if ((sig[c] + adjustment) > 255)
259               running_avg_uv[c] = 255;
260             else
261               running_avg_uv[c] = sig[c] + adjustment;
262             sum_diff += adjustment;
263           } else {
264             if ((sig[c] - adjustment) < 0)
265               running_avg_uv[c] = 0;
266             else
267               running_avg_uv[c] = sig[c] - adjustment;
268             sum_diff -= adjustment;
269           }
270         }
271       }
272       /* Update pointers for next iteration. */
273       sig += sig_stride;
274       mc_running_avg_uv += mc_avg_uv_stride;
275       running_avg_uv += avg_uv_stride;
276     }
277
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.
288
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.
292       if (delta < 4) {
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)
301               adjustment = delta;
302             if (diff > 0) {
303               // Bring denoised signal down.
304               if (running_avg_uv[c] - adjustment < 0)
305                 running_avg_uv[c] = 0;
306               else
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;
313               else
314                 running_avg_uv[c] = running_avg_uv[c] + adjustment;
315               sum_diff += adjustment;
316             }
317           }
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.
320           sig += sig_stride;
321           mc_running_avg_uv += mc_avg_uv_stride;
322           running_avg_uv += avg_uv_stride;
323         }
324         if (abs(sum_diff) > sum_diff_thresh)
325           return COPY_BLOCK;
326       } else {
327         return COPY_BLOCK;
328       }
329     }
330
331     vp8_copy_mem8x8(running_avg_uv_start, avg_uv_stride, sig_start,
332                     sig_stride);
333     return FILTER_BLOCK;
334 }
335
336 int vp8_denoiser_allocate(VP8_DENOISER *denoiser, int width, int height,
337                           int num_mb_rows, int num_mb_cols)
338 {
339     int i;
340     assert(denoiser);
341     denoiser->num_mb_cols = num_mb_cols;
342
343     for (i = 0; i < MAX_REF_FRAMES; i++)
344     {
345         denoiser->yv12_running_avg[i].flags = 0;
346
347         if (vp8_yv12_alloc_frame_buffer(&(denoiser->yv12_running_avg[i]), width,
348                                         height, VP8BORDERINPIXELS)
349             < 0)
350         {
351             vp8_denoiser_free(denoiser);
352             return 1;
353         }
354         vpx_memset(denoiser->yv12_running_avg[i].buffer_alloc, 0,
355                    denoiser->yv12_running_avg[i].frame_size);
356
357     }
358     denoiser->yv12_mc_running_avg.flags = 0;
359
360     if (vp8_yv12_alloc_frame_buffer(&(denoiser->yv12_mc_running_avg), width,
361                                    height, VP8BORDERINPIXELS) < 0)
362     {
363         vp8_denoiser_free(denoiser);
364         return 1;
365     }
366
367     vpx_memset(denoiser->yv12_mc_running_avg.buffer_alloc, 0,
368                denoiser->yv12_mc_running_avg.frame_size);
369
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));
372
373     return 0;
374 }
375
376 void vp8_denoiser_free(VP8_DENOISER *denoiser)
377 {
378     int i;
379     assert(denoiser);
380
381     for (i = 0; i < MAX_REF_FRAMES ; i++)
382     {
383         vp8_yv12_de_alloc_frame_buffer(&denoiser->yv12_running_avg[i]);
384     }
385     vp8_yv12_de_alloc_frame_buffer(&denoiser->yv12_mc_running_avg);
386     vpx_free(denoiser->denoise_state);
387 }
388
389
390 void vp8_denoiser_denoise_mb(VP8_DENOISER *denoiser,
391                              MACROBLOCK *x,
392                              unsigned int best_sse,
393                              unsigned int zero_mv_sse,
394                              int recon_yoffset,
395                              int recon_uvoffset,
396                              loop_filter_info_n *lfi_n,
397                              int mb_row,
398                              int mb_col,
399                              int block_index,
400                              int uv_denoise)
401 {
402     int mv_row;
403     int mv_col;
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;
412
413     enum vp8_denoiser_decision decision = FILTER_BLOCK;
414     enum vp8_denoiser_decision decision_u = COPY_BLOCK;
415     enum vp8_denoiser_decision decision_v = COPY_BLOCK;
416
417     if (zero_frame)
418     {
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;
425         int sse_diff = 0;
426         // Bias on zero motion vector sse.
427         int zero_bias = 95;
428         zero_mv_sse = (unsigned int)((int64_t)zero_mv_sse * zero_bias / 100);
429         sse_diff = zero_mv_sse - best_sse;
430
431         saved_mbmi = *mbmi;
432
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;
447
448         if (frame == INTRA_FRAME ||
449             sse_diff <= sse_diff_thresh)
450         {
451             /*
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
457              * denoising.
458              */
459             mbmi->ref_frame =
460                     x->best_zeromv_reference_frame;
461
462             src = &denoiser->yv12_running_avg[zero_frame];
463
464             mbmi->mode = ZEROMV;
465             mbmi->mv.as_int = 0;
466             x->best_sse_inter_mode = ZEROMV;
467             x->best_sse_mv.as_int = 0;
468             best_sse = zero_mv_sse;
469         }
470
471         saved_pre = filter_xd->pre;
472         saved_dst = filter_xd->dst;
473
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;
482
483         if (!x->skip)
484         {
485             vp8_build_inter_predictors_mb(filter_xd);
486         }
487         else
488         {
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);
495         }
496         filter_xd->pre = saved_pre;
497         filter_xd->dst = saved_dst;
498         *mbmi = saved_mbmi;
499
500     }
501
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;
507
508     if (best_sse > sse_thresh || motion_magnitude2
509            > 8 * NOISE_MOTION_THRESHOLD)
510     {
511         decision = COPY_BLOCK;
512     }
513
514     if (decision == FILTER_BLOCK)
515     {
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;
522
523         /* Filter. */
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.
531         if (uv_denoise &&
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;
545           decision_u =
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);
550           decision_v =
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);
555         }
556     }
557     if (decision == COPY_BLOCK)
558     {
559         /* No filtering of this block; it differs too much from the predictor,
560          * or the motion vector magnitude is considered too big.
561          */
562         vp8_copy_mem16x16(
563                 x->thismb, 16,
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;
567     }
568     if (uv_denoise) {
569       if (decision_u == COPY_BLOCK) {
570         vp8_copy_mem8x8(
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);
574       }
575       if (decision_v == COPY_BLOCK) {
576         vp8_copy_mem8x8(
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);
580       }
581     }
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;
590
591       // Fix filter level to some nominal value for now.
592       int filter_level = 32;
593
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];
599
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.
603       if (mb_col > 0) {
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.
609           apply_filter = 1;
610           vp8_loop_filter_mbv(
611               denoiser->yv12_running_avg[INTRA_FRAME].y_buffer + recon_yoffset,
612               NULL, NULL, y_stride, uv_stride, &lfi);
613         }
614       }
615       if (mb_row > 0) {
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.
621           apply_filter = 1;
622           vp8_loop_filter_mbh(
623               denoiser->yv12_running_avg[INTRA_FRAME].y_buffer + recon_yoffset,
624               NULL, NULL, y_stride, uv_stride, &lfi);
625         }
626       }
627       if (apply_filter) {
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.
630         vp8_copy_mem16x16(
631             denoiser->yv12_running_avg[INTRA_FRAME].y_buffer + recon_yoffset,
632             y_stride, x->thismb, 16);
633       }
634     }
635 }