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 <assert.h>
12 #include <limits.h>
13 #include <math.h>
14 
15 #include "./vpx_dsp_rtcd.h"
16 #include "vpx_dsp/vpx_dsp_common.h"
17 #include "vpx_scale/yv12config.h"
18 #include "vpx/vpx_integer.h"
19 #include "vp9/common/vp9_reconinter.h"
20 #include "vp9/encoder/vp9_context_tree.h"
21 #include "vp9/encoder/vp9_denoiser.h"
22 #include "vp9/encoder/vp9_encoder.h"
23 
24 #ifdef OUTPUT_YUV_DENOISED
25 static void make_grayscale(YV12_BUFFER_CONFIG *yuv);
26 #endif
27 
absdiff_thresh(BLOCK_SIZE bs,int increase_denoising)28 static int absdiff_thresh(BLOCK_SIZE bs, int increase_denoising) {
29   (void)bs;
30   return 3 + (increase_denoising ? 1 : 0);
31 }
32 
delta_thresh(BLOCK_SIZE bs,int increase_denoising)33 static int delta_thresh(BLOCK_SIZE bs, int increase_denoising) {
34   (void)bs;
35   (void)increase_denoising;
36   return 4;
37 }
38 
noise_motion_thresh(BLOCK_SIZE bs,int increase_denoising)39 static int noise_motion_thresh(BLOCK_SIZE bs, int increase_denoising) {
40   (void)bs;
41   (void)increase_denoising;
42   return 625;
43 }
44 
sse_thresh(BLOCK_SIZE bs,int increase_denoising)45 static unsigned int sse_thresh(BLOCK_SIZE bs, int increase_denoising) {
46   return (1 << num_pels_log2_lookup[bs]) * (increase_denoising ? 80 : 40);
47 }
48 
sse_diff_thresh(BLOCK_SIZE bs,int increase_denoising,int motion_magnitude)49 static int sse_diff_thresh(BLOCK_SIZE bs, int increase_denoising,
50                            int motion_magnitude) {
51   if (motion_magnitude > noise_motion_thresh(bs, increase_denoising)) {
52     if (increase_denoising)
53       return (1 << num_pels_log2_lookup[bs]) << 2;
54     else
55       return 0;
56   } else {
57     return (1 << num_pels_log2_lookup[bs]) << 4;
58   }
59 }
60 
total_adj_weak_thresh(BLOCK_SIZE bs,int increase_denoising)61 static int total_adj_weak_thresh(BLOCK_SIZE bs, int increase_denoising) {
62   return (1 << num_pels_log2_lookup[bs]) * (increase_denoising ? 3 : 2);
63 }
64 
65 // TODO(jackychen): If increase_denoising is enabled in the future,
66 // we might need to update the code for calculating 'total_adj' in
67 // case the C code is not bit-exact with corresponding sse2 code.
vp9_denoiser_filter_c(const uint8_t * sig,int sig_stride,const uint8_t * mc_avg,int mc_avg_stride,uint8_t * avg,int avg_stride,int increase_denoising,BLOCK_SIZE bs,int motion_magnitude)68 int vp9_denoiser_filter_c(const uint8_t *sig, int sig_stride,
69                           const uint8_t *mc_avg, int mc_avg_stride,
70                           uint8_t *avg, int avg_stride, int increase_denoising,
71                           BLOCK_SIZE bs, int motion_magnitude) {
72   int r, c;
73   const uint8_t *sig_start = sig;
74   const uint8_t *mc_avg_start = mc_avg;
75   uint8_t *avg_start = avg;
76   int diff, adj, absdiff, delta;
77   int adj_val[] = { 3, 4, 6 };
78   int total_adj = 0;
79   int shift_inc = 1;
80 
81   // If motion_magnitude is small, making the denoiser more aggressive by
82   // increasing the adjustment for each level. Add another increment for
83   // blocks that are labeled for increase denoising.
84   if (motion_magnitude <= MOTION_MAGNITUDE_THRESHOLD) {
85     if (increase_denoising) {
86       shift_inc = 2;
87     }
88     adj_val[0] += shift_inc;
89     adj_val[1] += shift_inc;
90     adj_val[2] += shift_inc;
91   }
92 
93   // First attempt to apply a strong temporal denoising filter.
94   for (r = 0; r < (4 << b_height_log2_lookup[bs]); ++r) {
95     for (c = 0; c < (4 << b_width_log2_lookup[bs]); ++c) {
96       diff = mc_avg[c] - sig[c];
97       absdiff = abs(diff);
98 
99       if (absdiff <= absdiff_thresh(bs, increase_denoising)) {
100         avg[c] = mc_avg[c];
101         total_adj += diff;
102       } else {
103         switch (absdiff) {
104           case 4:
105           case 5:
106           case 6:
107           case 7: adj = adj_val[0]; break;
108           case 8:
109           case 9:
110           case 10:
111           case 11:
112           case 12:
113           case 13:
114           case 14:
115           case 15: adj = adj_val[1]; break;
116           default: adj = adj_val[2];
117         }
118         if (diff > 0) {
119           avg[c] = VPXMIN(UINT8_MAX, sig[c] + adj);
120           total_adj += adj;
121         } else {
122           avg[c] = VPXMAX(0, sig[c] - adj);
123           total_adj -= adj;
124         }
125       }
126     }
127     sig += sig_stride;
128     avg += avg_stride;
129     mc_avg += mc_avg_stride;
130   }
131 
132   // If the strong filter did not modify the signal too much, we're all set.
133   if (abs(total_adj) <= total_adj_strong_thresh(bs, increase_denoising)) {
134     return FILTER_BLOCK;
135   }
136 
137   // Otherwise, we try to dampen the filter if the delta is not too high.
138   delta = ((abs(total_adj) - total_adj_strong_thresh(bs, increase_denoising)) >>
139            num_pels_log2_lookup[bs]) +
140           1;
141 
142   if (delta >= delta_thresh(bs, increase_denoising)) {
143     return COPY_BLOCK;
144   }
145 
146   mc_avg = mc_avg_start;
147   avg = avg_start;
148   sig = sig_start;
149   for (r = 0; r < (4 << b_height_log2_lookup[bs]); ++r) {
150     for (c = 0; c < (4 << b_width_log2_lookup[bs]); ++c) {
151       diff = mc_avg[c] - sig[c];
152       adj = abs(diff);
153       if (adj > delta) {
154         adj = delta;
155       }
156       if (diff > 0) {
157         // Diff positive means we made positive adjustment above
158         // (in first try/attempt), so now make negative adjustment to bring
159         // denoised signal down.
160         avg[c] = VPXMAX(0, avg[c] - adj);
161         total_adj -= adj;
162       } else {
163         // Diff negative means we made negative adjustment above
164         // (in first try/attempt), so now make positive adjustment to bring
165         // denoised signal up.
166         avg[c] = VPXMIN(UINT8_MAX, avg[c] + adj);
167         total_adj += adj;
168       }
169     }
170     sig += sig_stride;
171     avg += avg_stride;
172     mc_avg += mc_avg_stride;
173   }
174 
175   // We can use the filter if it has been sufficiently dampened
176   if (abs(total_adj) <= total_adj_weak_thresh(bs, increase_denoising)) {
177     return FILTER_BLOCK;
178   }
179   return COPY_BLOCK;
180 }
181 
block_start(uint8_t * framebuf,int stride,int mi_row,int mi_col)182 static uint8_t *block_start(uint8_t *framebuf, int stride, int mi_row,
183                             int mi_col) {
184   return framebuf + (stride * mi_row << 3) + (mi_col << 3);
185 }
186 
perform_motion_compensation(VP9_COMMON * const cm,VP9_DENOISER * denoiser,MACROBLOCK * mb,BLOCK_SIZE bs,int increase_denoising,int mi_row,int mi_col,PICK_MODE_CONTEXT * ctx,int motion_magnitude,int is_skin,int * zeromv_filter,int consec_zeromv,int num_spatial_layers,int width,int lst_fb_idx,int gld_fb_idx,int use_svc,int spatial_layer,int use_gf_temporal_ref)187 static VP9_DENOISER_DECISION perform_motion_compensation(
188     VP9_COMMON *const cm, VP9_DENOISER *denoiser, MACROBLOCK *mb, BLOCK_SIZE bs,
189     int increase_denoising, int mi_row, int mi_col, PICK_MODE_CONTEXT *ctx,
190     int motion_magnitude, int is_skin, int *zeromv_filter, int consec_zeromv,
191     int num_spatial_layers, int width, int lst_fb_idx, int gld_fb_idx,
192     int use_svc, int spatial_layer, int use_gf_temporal_ref) {
193   const int sse_diff = (ctx->newmv_sse == UINT_MAX)
194                            ? 0
195                            : ((int)ctx->zeromv_sse - (int)ctx->newmv_sse);
196   int frame;
197   int denoise_layer_idx = 0;
198   MACROBLOCKD *filter_mbd = &mb->e_mbd;
199   MODE_INFO *mi = filter_mbd->mi[0];
200   MODE_INFO saved_mi;
201   int i;
202   struct buf_2d saved_dst[MAX_MB_PLANE];
203   struct buf_2d saved_pre[MAX_MB_PLANE];
204   const RefBuffer *saved_block_refs[2];
205   MV_REFERENCE_FRAME saved_frame;
206 
207   frame = ctx->best_reference_frame;
208 
209   saved_mi = *mi;
210 
211   if (is_skin && (motion_magnitude > 0 || consec_zeromv < 4)) return COPY_BLOCK;
212 
213   // Avoid denoising small blocks. When noise > kDenLow or frame width > 480,
214   // denoise 16x16 blocks.
215   if (bs == BLOCK_8X8 || bs == BLOCK_8X16 || bs == BLOCK_16X8 ||
216       (bs == BLOCK_16X16 && width > 480 &&
217        denoiser->denoising_level <= kDenLow))
218     return COPY_BLOCK;
219 
220   // If the best reference frame uses inter-prediction and there is enough of a
221   // difference in sum-squared-error, use it.
222   if (frame != INTRA_FRAME && frame != ALTREF_FRAME && frame != GOLDEN_FRAME &&
223       sse_diff > sse_diff_thresh(bs, increase_denoising, motion_magnitude)) {
224     mi->ref_frame[0] = ctx->best_reference_frame;
225     mi->mode = ctx->best_sse_inter_mode;
226     mi->mv[0] = ctx->best_sse_mv;
227   } else {
228     // Otherwise, use the zero reference frame.
229     frame = ctx->best_zeromv_reference_frame;
230     ctx->newmv_sse = ctx->zeromv_sse;
231     // Bias to last reference.
232     if ((num_spatial_layers > 1 && !use_gf_temporal_ref) ||
233         frame == ALTREF_FRAME ||
234         (frame == GOLDEN_FRAME && use_gf_temporal_ref) ||
235         (frame != LAST_FRAME &&
236          ((ctx->zeromv_lastref_sse<(5 * ctx->zeromv_sse)>> 2) ||
237           denoiser->denoising_level >= kDenHigh))) {
238       frame = LAST_FRAME;
239       ctx->newmv_sse = ctx->zeromv_lastref_sse;
240     }
241     mi->ref_frame[0] = frame;
242     mi->mode = ZEROMV;
243     mi->mv[0].as_int = 0;
244     ctx->best_sse_inter_mode = ZEROMV;
245     ctx->best_sse_mv.as_int = 0;
246     *zeromv_filter = 1;
247     if (denoiser->denoising_level > kDenMedium) {
248       motion_magnitude = 0;
249     }
250   }
251 
252   saved_frame = frame;
253   // When using SVC, we need to map REF_FRAME to the frame buffer index.
254   if (use_svc) {
255     if (frame == LAST_FRAME)
256       frame = lst_fb_idx + 1;
257     else if (frame == GOLDEN_FRAME)
258       frame = gld_fb_idx + 1;
259     // Shift for the second spatial layer.
260     if (num_spatial_layers - spatial_layer == 2)
261       frame = frame + denoiser->num_ref_frames;
262     denoise_layer_idx = num_spatial_layers - spatial_layer - 1;
263   }
264 
265   // Force copy (no denoise, copy source in denoised buffer) if
266   // running_avg_y[frame] is NULL.
267   if (denoiser->running_avg_y[frame].buffer_alloc == NULL) {
268     // Restore everything to its original state
269     *mi = saved_mi;
270     return COPY_BLOCK;
271   }
272 
273   if (ctx->newmv_sse > sse_thresh(bs, increase_denoising)) {
274     // Restore everything to its original state
275     *mi = saved_mi;
276     return COPY_BLOCK;
277   }
278   if (motion_magnitude > (noise_motion_thresh(bs, increase_denoising) << 3)) {
279     // Restore everything to its original state
280     *mi = saved_mi;
281     return COPY_BLOCK;
282   }
283 
284   // We will restore these after motion compensation.
285   for (i = 0; i < MAX_MB_PLANE; ++i) {
286     saved_pre[i] = filter_mbd->plane[i].pre[0];
287     saved_dst[i] = filter_mbd->plane[i].dst;
288   }
289   saved_block_refs[0] = filter_mbd->block_refs[0];
290 
291   // Set the pointers in the MACROBLOCKD to point to the buffers in the denoiser
292   // struct.
293   filter_mbd->plane[0].pre[0].buf =
294       block_start(denoiser->running_avg_y[frame].y_buffer,
295                   denoiser->running_avg_y[frame].y_stride, mi_row, mi_col);
296   filter_mbd->plane[0].pre[0].stride = denoiser->running_avg_y[frame].y_stride;
297   filter_mbd->plane[1].pre[0].buf =
298       block_start(denoiser->running_avg_y[frame].u_buffer,
299                   denoiser->running_avg_y[frame].uv_stride, mi_row, mi_col);
300   filter_mbd->plane[1].pre[0].stride = denoiser->running_avg_y[frame].uv_stride;
301   filter_mbd->plane[2].pre[0].buf =
302       block_start(denoiser->running_avg_y[frame].v_buffer,
303                   denoiser->running_avg_y[frame].uv_stride, mi_row, mi_col);
304   filter_mbd->plane[2].pre[0].stride = denoiser->running_avg_y[frame].uv_stride;
305 
306   filter_mbd->plane[0].dst.buf = block_start(
307       denoiser->mc_running_avg_y[denoise_layer_idx].y_buffer,
308       denoiser->mc_running_avg_y[denoise_layer_idx].y_stride, mi_row, mi_col);
309   filter_mbd->plane[0].dst.stride =
310       denoiser->mc_running_avg_y[denoise_layer_idx].y_stride;
311   filter_mbd->plane[1].dst.buf = block_start(
312       denoiser->mc_running_avg_y[denoise_layer_idx].u_buffer,
313       denoiser->mc_running_avg_y[denoise_layer_idx].uv_stride, mi_row, mi_col);
314   filter_mbd->plane[1].dst.stride =
315       denoiser->mc_running_avg_y[denoise_layer_idx].uv_stride;
316   filter_mbd->plane[2].dst.buf = block_start(
317       denoiser->mc_running_avg_y[denoise_layer_idx].v_buffer,
318       denoiser->mc_running_avg_y[denoise_layer_idx].uv_stride, mi_row, mi_col);
319   filter_mbd->plane[2].dst.stride =
320       denoiser->mc_running_avg_y[denoise_layer_idx].uv_stride;
321 
322   set_ref_ptrs(cm, filter_mbd, saved_frame, NONE);
323   vp9_build_inter_predictors_sby(filter_mbd, mi_row, mi_col, bs);
324 
325   // Restore everything to its original state
326   *mi = saved_mi;
327   filter_mbd->block_refs[0] = saved_block_refs[0];
328   for (i = 0; i < MAX_MB_PLANE; ++i) {
329     filter_mbd->plane[i].pre[0] = saved_pre[i];
330     filter_mbd->plane[i].dst = saved_dst[i];
331   }
332 
333   return FILTER_BLOCK;
334 }
335 
vp9_denoiser_denoise(VP9_COMP * cpi,MACROBLOCK * mb,int mi_row,int mi_col,BLOCK_SIZE bs,PICK_MODE_CONTEXT * ctx,VP9_DENOISER_DECISION * denoiser_decision,int use_gf_temporal_ref)336 void vp9_denoiser_denoise(VP9_COMP *cpi, MACROBLOCK *mb, int mi_row, int mi_col,
337                           BLOCK_SIZE bs, PICK_MODE_CONTEXT *ctx,
338                           VP9_DENOISER_DECISION *denoiser_decision,
339                           int use_gf_temporal_ref) {
340   int mv_col, mv_row;
341   int motion_magnitude = 0;
342   int zeromv_filter = 0;
343   VP9_DENOISER *denoiser = &cpi->denoiser;
344   VP9_DENOISER_DECISION decision = COPY_BLOCK;
345 
346   const int shift =
347       cpi->svc.number_spatial_layers - cpi->svc.spatial_layer_id == 2
348           ? denoiser->num_ref_frames
349           : 0;
350   YV12_BUFFER_CONFIG avg = denoiser->running_avg_y[INTRA_FRAME + shift];
351   const int denoise_layer_index =
352       cpi->svc.number_spatial_layers - cpi->svc.spatial_layer_id - 1;
353   YV12_BUFFER_CONFIG mc_avg = denoiser->mc_running_avg_y[denoise_layer_index];
354   uint8_t *avg_start = block_start(avg.y_buffer, avg.y_stride, mi_row, mi_col);
355 
356   uint8_t *mc_avg_start =
357       block_start(mc_avg.y_buffer, mc_avg.y_stride, mi_row, mi_col);
358   struct buf_2d src = mb->plane[0].src;
359   int is_skin = 0;
360   int increase_denoising = 0;
361   int consec_zeromv = 0;
362   int last_is_reference = cpi->ref_frame_flags & VP9_LAST_FLAG;
363   mv_col = ctx->best_sse_mv.as_mv.col;
364   mv_row = ctx->best_sse_mv.as_mv.row;
365   motion_magnitude = mv_row * mv_row + mv_col * mv_col;
366 
367   if (cpi->use_skin_detection && bs <= BLOCK_32X32 &&
368       denoiser->denoising_level < kDenHigh) {
369     int motion_level = (motion_magnitude < 16) ? 0 : 1;
370     // If motion for current block is small/zero, compute consec_zeromv for
371     // skin detection (early exit in skin detection is done for large
372     // consec_zeromv when current block has small/zero motion).
373     consec_zeromv = 0;
374     if (motion_level == 0) {
375       VP9_COMMON *const cm = &cpi->common;
376       int j, i;
377       // Loop through the 8x8 sub-blocks.
378       const int bw = num_8x8_blocks_wide_lookup[bs];
379       const int bh = num_8x8_blocks_high_lookup[bs];
380       const int xmis = VPXMIN(cm->mi_cols - mi_col, bw);
381       const int ymis = VPXMIN(cm->mi_rows - mi_row, bh);
382       const int block_index = mi_row * cm->mi_cols + mi_col;
383       consec_zeromv = 100;
384       for (i = 0; i < ymis; i++) {
385         for (j = 0; j < xmis; j++) {
386           int bl_index = block_index + i * cm->mi_cols + j;
387           consec_zeromv = VPXMIN(cpi->consec_zero_mv[bl_index], consec_zeromv);
388           // No need to keep checking 8x8 blocks if any of the sub-blocks
389           // has small consec_zeromv (since threshold for no_skin based on
390           // zero/small motion in skin detection is high, i.e, > 4).
391           if (consec_zeromv < 4) {
392             i = ymis;
393             break;
394           }
395         }
396       }
397     }
398     // TODO(marpan): Compute skin detection over sub-blocks.
399     is_skin = vp9_compute_skin_block(
400         mb->plane[0].src.buf, mb->plane[1].src.buf, mb->plane[2].src.buf,
401         mb->plane[0].src.stride, mb->plane[1].src.stride, bs, consec_zeromv,
402         motion_level);
403   }
404   if (!is_skin && denoiser->denoising_level == kDenHigh) increase_denoising = 1;
405 
406   // Copy block if LAST_FRAME is not a reference.
407   // Last doesn't always exist when SVC layers are dynamically changed, e.g. top
408   // spatial layer doesn't have last reference when it's brought up for the
409   // first time on the fly.
410   if (last_is_reference && denoiser->denoising_level >= kDenLow &&
411       !ctx->sb_skip_denoising)
412     decision = perform_motion_compensation(
413         &cpi->common, denoiser, mb, bs, increase_denoising, mi_row, mi_col, ctx,
414         motion_magnitude, is_skin, &zeromv_filter, consec_zeromv,
415         cpi->svc.number_spatial_layers, cpi->Source->y_width, cpi->lst_fb_idx,
416         cpi->gld_fb_idx, cpi->use_svc, cpi->svc.spatial_layer_id,
417         use_gf_temporal_ref);
418 
419   if (decision == FILTER_BLOCK) {
420     decision = vp9_denoiser_filter(src.buf, src.stride, mc_avg_start,
421                                    mc_avg.y_stride, avg_start, avg.y_stride,
422                                    increase_denoising, bs, motion_magnitude);
423   }
424 
425   if (decision == FILTER_BLOCK) {
426     vpx_convolve_copy(avg_start, avg.y_stride, src.buf, src.stride, NULL, 0, 0,
427                       0, 0, num_4x4_blocks_wide_lookup[bs] << 2,
428                       num_4x4_blocks_high_lookup[bs] << 2);
429   } else {  // COPY_BLOCK
430     vpx_convolve_copy(src.buf, src.stride, avg_start, avg.y_stride, NULL, 0, 0,
431                       0, 0, num_4x4_blocks_wide_lookup[bs] << 2,
432                       num_4x4_blocks_high_lookup[bs] << 2);
433   }
434   *denoiser_decision = decision;
435   if (decision == FILTER_BLOCK && zeromv_filter == 1)
436     *denoiser_decision = FILTER_ZEROMV_BLOCK;
437 }
438 
copy_frame(YV12_BUFFER_CONFIG * const dest,const YV12_BUFFER_CONFIG * const src)439 static void copy_frame(YV12_BUFFER_CONFIG *const dest,
440                        const YV12_BUFFER_CONFIG *const src) {
441   int r;
442   const uint8_t *srcbuf = src->y_buffer;
443   uint8_t *destbuf = dest->y_buffer;
444 
445   assert(dest->y_width == src->y_width);
446   assert(dest->y_height == src->y_height);
447 
448   for (r = 0; r < dest->y_height; ++r) {
449     memcpy(destbuf, srcbuf, dest->y_width);
450     destbuf += dest->y_stride;
451     srcbuf += src->y_stride;
452   }
453 }
454 
swap_frame_buffer(YV12_BUFFER_CONFIG * const dest,YV12_BUFFER_CONFIG * const src)455 static void swap_frame_buffer(YV12_BUFFER_CONFIG *const dest,
456                               YV12_BUFFER_CONFIG *const src) {
457   uint8_t *tmp_buf = dest->y_buffer;
458   assert(dest->y_width == src->y_width);
459   assert(dest->y_height == src->y_height);
460   dest->y_buffer = src->y_buffer;
461   src->y_buffer = tmp_buf;
462 }
463 
vp9_denoiser_update_frame_info(VP9_DENOISER * denoiser,YV12_BUFFER_CONFIG src,struct SVC * svc,FRAME_TYPE frame_type,int refresh_alt_ref_frame,int refresh_golden_frame,int refresh_last_frame,int alt_fb_idx,int gld_fb_idx,int lst_fb_idx,int resized,int svc_refresh_denoiser_buffers,int second_spatial_layer)464 void vp9_denoiser_update_frame_info(
465     VP9_DENOISER *denoiser, YV12_BUFFER_CONFIG src, struct SVC *svc,
466     FRAME_TYPE frame_type, int refresh_alt_ref_frame, int refresh_golden_frame,
467     int refresh_last_frame, int alt_fb_idx, int gld_fb_idx, int lst_fb_idx,
468     int resized, int svc_refresh_denoiser_buffers, int second_spatial_layer) {
469   const int shift = second_spatial_layer ? denoiser->num_ref_frames : 0;
470   // Copy source into denoised reference buffers on KEY_FRAME or
471   // if the just encoded frame was resized. For SVC, copy source if the base
472   // spatial layer was key frame.
473   if (frame_type == KEY_FRAME || resized != 0 || denoiser->reset ||
474       svc_refresh_denoiser_buffers) {
475     int i;
476     // Start at 1 so as not to overwrite the INTRA_FRAME
477     for (i = 1; i < denoiser->num_ref_frames; ++i) {
478       if (denoiser->running_avg_y[i + shift].buffer_alloc != NULL)
479         copy_frame(&denoiser->running_avg_y[i + shift], &src);
480     }
481     denoiser->reset = 0;
482     return;
483   }
484 
485   if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_BYPASS &&
486       svc->use_set_ref_frame_config) {
487     int i;
488     for (i = 0; i < REF_FRAMES; i++) {
489       if (svc->update_buffer_slot[svc->spatial_layer_id] & (1 << i))
490         copy_frame(&denoiser->running_avg_y[i + 1 + shift],
491                    &denoiser->running_avg_y[INTRA_FRAME + shift]);
492     }
493   } else {
494     // If more than one refresh occurs, must copy frame buffer.
495     if ((refresh_alt_ref_frame + refresh_golden_frame + refresh_last_frame) >
496         1) {
497       if (refresh_alt_ref_frame) {
498         copy_frame(&denoiser->running_avg_y[alt_fb_idx + 1 + shift],
499                    &denoiser->running_avg_y[INTRA_FRAME + shift]);
500       }
501       if (refresh_golden_frame) {
502         copy_frame(&denoiser->running_avg_y[gld_fb_idx + 1 + shift],
503                    &denoiser->running_avg_y[INTRA_FRAME + shift]);
504       }
505       if (refresh_last_frame) {
506         copy_frame(&denoiser->running_avg_y[lst_fb_idx + 1 + shift],
507                    &denoiser->running_avg_y[INTRA_FRAME + shift]);
508       }
509     } else {
510       if (refresh_alt_ref_frame) {
511         swap_frame_buffer(&denoiser->running_avg_y[alt_fb_idx + 1 + shift],
512                           &denoiser->running_avg_y[INTRA_FRAME + shift]);
513       }
514       if (refresh_golden_frame) {
515         swap_frame_buffer(&denoiser->running_avg_y[gld_fb_idx + 1 + shift],
516                           &denoiser->running_avg_y[INTRA_FRAME + shift]);
517       }
518       if (refresh_last_frame) {
519         swap_frame_buffer(&denoiser->running_avg_y[lst_fb_idx + 1 + shift],
520                           &denoiser->running_avg_y[INTRA_FRAME + shift]);
521       }
522     }
523   }
524 }
525 
vp9_denoiser_reset_frame_stats(PICK_MODE_CONTEXT * ctx)526 void vp9_denoiser_reset_frame_stats(PICK_MODE_CONTEXT *ctx) {
527   ctx->zeromv_sse = UINT_MAX;
528   ctx->newmv_sse = UINT_MAX;
529   ctx->zeromv_lastref_sse = UINT_MAX;
530   ctx->best_sse_mv.as_int = 0;
531 }
532 
vp9_denoiser_update_frame_stats(MODE_INFO * mi,unsigned int sse,PREDICTION_MODE mode,PICK_MODE_CONTEXT * ctx)533 void vp9_denoiser_update_frame_stats(MODE_INFO *mi, unsigned int sse,
534                                      PREDICTION_MODE mode,
535                                      PICK_MODE_CONTEXT *ctx) {
536   if (mi->mv[0].as_int == 0 && sse < ctx->zeromv_sse) {
537     ctx->zeromv_sse = sse;
538     ctx->best_zeromv_reference_frame = mi->ref_frame[0];
539     if (mi->ref_frame[0] == LAST_FRAME) ctx->zeromv_lastref_sse = sse;
540   }
541 
542   if (mi->mv[0].as_int != 0 && sse < ctx->newmv_sse) {
543     ctx->newmv_sse = sse;
544     ctx->best_sse_inter_mode = mode;
545     ctx->best_sse_mv = mi->mv[0];
546     ctx->best_reference_frame = mi->ref_frame[0];
547   }
548 }
549 
vp9_denoiser_realloc_svc_helper(VP9_COMMON * cm,VP9_DENOISER * denoiser,int fb_idx)550 static int vp9_denoiser_realloc_svc_helper(VP9_COMMON *cm,
551                                            VP9_DENOISER *denoiser, int fb_idx) {
552   int fail = 0;
553   if (denoiser->running_avg_y[fb_idx].buffer_alloc == NULL) {
554     fail =
555         vpx_alloc_frame_buffer(&denoiser->running_avg_y[fb_idx], cm->width,
556                                cm->height, cm->subsampling_x, cm->subsampling_y,
557 #if CONFIG_VP9_HIGHBITDEPTH
558                                cm->use_highbitdepth,
559 #endif
560                                VP9_ENC_BORDER_IN_PIXELS, 0);
561     if (fail) {
562       vp9_denoiser_free(denoiser);
563       return 1;
564     }
565   }
566   return 0;
567 }
568 
vp9_denoiser_realloc_svc(VP9_COMMON * cm,VP9_DENOISER * denoiser,struct SVC * svc,int svc_buf_shift,int refresh_alt,int refresh_gld,int refresh_lst,int alt_fb_idx,int gld_fb_idx,int lst_fb_idx)569 int vp9_denoiser_realloc_svc(VP9_COMMON *cm, VP9_DENOISER *denoiser,
570                              struct SVC *svc, int svc_buf_shift,
571                              int refresh_alt, int refresh_gld, int refresh_lst,
572                              int alt_fb_idx, int gld_fb_idx, int lst_fb_idx) {
573   int fail = 0;
574   if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_BYPASS &&
575       svc->use_set_ref_frame_config) {
576     int i;
577     for (i = 0; i < REF_FRAMES; i++) {
578       if (cm->frame_type == KEY_FRAME ||
579           svc->update_buffer_slot[svc->spatial_layer_id] & (1 << i)) {
580         fail = vp9_denoiser_realloc_svc_helper(cm, denoiser,
581                                                i + 1 + svc_buf_shift);
582       }
583     }
584   } else {
585     if (refresh_alt) {
586       // Increase the frame buffer index by 1 to map it to the buffer index in
587       // the denoiser.
588       fail = vp9_denoiser_realloc_svc_helper(cm, denoiser,
589                                              alt_fb_idx + 1 + svc_buf_shift);
590       if (fail) return 1;
591     }
592     if (refresh_gld) {
593       fail = vp9_denoiser_realloc_svc_helper(cm, denoiser,
594                                              gld_fb_idx + 1 + svc_buf_shift);
595       if (fail) return 1;
596     }
597     if (refresh_lst) {
598       fail = vp9_denoiser_realloc_svc_helper(cm, denoiser,
599                                              lst_fb_idx + 1 + svc_buf_shift);
600       if (fail) return 1;
601     }
602   }
603   return 0;
604 }
605 
vp9_denoiser_alloc(VP9_COMMON * cm,struct SVC * svc,VP9_DENOISER * denoiser,int use_svc,int noise_sen,int width,int height,int ssx,int ssy,int use_highbitdepth,int border)606 int vp9_denoiser_alloc(VP9_COMMON *cm, struct SVC *svc, VP9_DENOISER *denoiser,
607                        int use_svc, int noise_sen, int width, int height,
608                        int ssx, int ssy,
609 #if CONFIG_VP9_HIGHBITDEPTH
610                        int use_highbitdepth,
611 #endif
612                        int border) {
613   int i, layer, fail, init_num_ref_frames;
614   const int legacy_byte_alignment = 0;
615   int num_layers = 1;
616   int scaled_width = width;
617   int scaled_height = height;
618   if (use_svc) {
619     LAYER_CONTEXT *lc = &svc->layer_context[svc->spatial_layer_id *
620                                                 svc->number_temporal_layers +
621                                             svc->temporal_layer_id];
622     get_layer_resolution(width, height, lc->scaling_factor_num,
623                          lc->scaling_factor_den, &scaled_width, &scaled_height);
624     // For SVC: only denoise at most 2 spatial (highest) layers.
625     if (noise_sen >= 2)
626       // Denoise from one spatial layer below the top.
627       svc->first_layer_denoise = VPXMAX(svc->number_spatial_layers - 2, 0);
628     else
629       // Only denoise the top spatial layer.
630       svc->first_layer_denoise = VPXMAX(svc->number_spatial_layers - 1, 0);
631     num_layers = svc->number_spatial_layers - svc->first_layer_denoise;
632   }
633   assert(denoiser != NULL);
634   denoiser->num_ref_frames = use_svc ? SVC_REF_FRAMES : NONSVC_REF_FRAMES;
635   init_num_ref_frames = use_svc ? MAX_REF_FRAMES : NONSVC_REF_FRAMES;
636   denoiser->num_layers = num_layers;
637   CHECK_MEM_ERROR(cm, denoiser->running_avg_y,
638                   vpx_calloc(denoiser->num_ref_frames * num_layers,
639                              sizeof(denoiser->running_avg_y[0])));
640   CHECK_MEM_ERROR(
641       cm, denoiser->mc_running_avg_y,
642       vpx_calloc(num_layers, sizeof(denoiser->mc_running_avg_y[0])));
643 
644   for (layer = 0; layer < num_layers; ++layer) {
645     const int denoise_width = (layer == 0) ? width : scaled_width;
646     const int denoise_height = (layer == 0) ? height : scaled_height;
647     for (i = 0; i < init_num_ref_frames; ++i) {
648       fail = vpx_alloc_frame_buffer(
649           &denoiser->running_avg_y[i + denoiser->num_ref_frames * layer],
650           denoise_width, denoise_height, ssx, ssy,
651 #if CONFIG_VP9_HIGHBITDEPTH
652           use_highbitdepth,
653 #endif
654           border, legacy_byte_alignment);
655       if (fail) {
656         vp9_denoiser_free(denoiser);
657         return 1;
658       }
659 #ifdef OUTPUT_YUV_DENOISED
660       make_grayscale(&denoiser->running_avg_y[i]);
661 #endif
662     }
663 
664     fail = vpx_alloc_frame_buffer(&denoiser->mc_running_avg_y[layer],
665                                   denoise_width, denoise_height, ssx, ssy,
666 #if CONFIG_VP9_HIGHBITDEPTH
667                                   use_highbitdepth,
668 #endif
669                                   border, legacy_byte_alignment);
670     if (fail) {
671       vp9_denoiser_free(denoiser);
672       return 1;
673     }
674   }
675 
676   // denoiser->last_source only used for noise_estimation, so only for top
677   // layer.
678   fail = vpx_alloc_frame_buffer(&denoiser->last_source, width, height, ssx, ssy,
679 #if CONFIG_VP9_HIGHBITDEPTH
680                                 use_highbitdepth,
681 #endif
682                                 border, legacy_byte_alignment);
683   if (fail) {
684     vp9_denoiser_free(denoiser);
685     return 1;
686   }
687 #ifdef OUTPUT_YUV_DENOISED
688   make_grayscale(&denoiser->running_avg_y[i]);
689 #endif
690   denoiser->frame_buffer_initialized = 1;
691   denoiser->denoising_level = kDenMedium;
692   denoiser->prev_denoising_level = kDenMedium;
693   denoiser->reset = 0;
694   denoiser->current_denoiser_frame = 0;
695   return 0;
696 }
697 
vp9_denoiser_free(VP9_DENOISER * denoiser)698 void vp9_denoiser_free(VP9_DENOISER *denoiser) {
699   int i;
700   if (denoiser == NULL) {
701     return;
702   }
703   denoiser->frame_buffer_initialized = 0;
704   for (i = 0; i < denoiser->num_ref_frames * denoiser->num_layers; ++i) {
705     vpx_free_frame_buffer(&denoiser->running_avg_y[i]);
706   }
707   vpx_free(denoiser->running_avg_y);
708   denoiser->running_avg_y = NULL;
709 
710   for (i = 0; i < denoiser->num_layers; ++i) {
711     vpx_free_frame_buffer(&denoiser->mc_running_avg_y[i]);
712   }
713 
714   vpx_free(denoiser->mc_running_avg_y);
715   denoiser->mc_running_avg_y = NULL;
716   vpx_free_frame_buffer(&denoiser->last_source);
717 }
718 
force_refresh_longterm_ref(VP9_COMP * const cpi)719 static void force_refresh_longterm_ref(VP9_COMP *const cpi) {
720   SVC *const svc = &cpi->svc;
721   // If long term reference is used, force refresh of that slot, so
722   // denoiser buffer for long term reference stays in sync.
723   if (svc->use_gf_temporal_ref_current_layer) {
724     int index = svc->spatial_layer_id;
725     if (svc->number_spatial_layers == 3) index = svc->spatial_layer_id - 1;
726     assert(index >= 0);
727     cpi->alt_fb_idx = svc->buffer_gf_temporal_ref[index].idx;
728     cpi->refresh_alt_ref_frame = 1;
729   }
730 }
731 
vp9_denoiser_set_noise_level(VP9_COMP * const cpi,int noise_level)732 void vp9_denoiser_set_noise_level(VP9_COMP *const cpi, int noise_level) {
733   VP9_DENOISER *const denoiser = &cpi->denoiser;
734   denoiser->denoising_level = noise_level;
735   if (denoiser->denoising_level > kDenLowLow &&
736       denoiser->prev_denoising_level == kDenLowLow) {
737     denoiser->reset = 1;
738     force_refresh_longterm_ref(cpi);
739   } else {
740     denoiser->reset = 0;
741   }
742   denoiser->prev_denoising_level = denoiser->denoising_level;
743 }
744 
745 // Scale/increase the partition threshold
746 // for denoiser speed-up.
vp9_scale_part_thresh(int64_t threshold,VP9_DENOISER_LEVEL noise_level,int content_state,int temporal_layer_id)747 int64_t vp9_scale_part_thresh(int64_t threshold, VP9_DENOISER_LEVEL noise_level,
748                               int content_state, int temporal_layer_id) {
749   if ((content_state == kLowSadLowSumdiff) ||
750       (content_state == kHighSadLowSumdiff) ||
751       (content_state == kLowVarHighSumdiff) || (noise_level == kDenHigh) ||
752       (temporal_layer_id != 0)) {
753     int64_t scaled_thr =
754         (temporal_layer_id < 2) ? (3 * threshold) >> 1 : (7 * threshold) >> 2;
755     return scaled_thr;
756   } else {
757     return (5 * threshold) >> 2;
758   }
759 }
760 
761 //  Scale/increase the ac skip threshold for
762 //  denoiser speed-up.
vp9_scale_acskip_thresh(int64_t threshold,VP9_DENOISER_LEVEL noise_level,int abs_sumdiff,int temporal_layer_id)763 int64_t vp9_scale_acskip_thresh(int64_t threshold,
764                                 VP9_DENOISER_LEVEL noise_level, int abs_sumdiff,
765                                 int temporal_layer_id) {
766   if (noise_level >= kDenLow && abs_sumdiff < 5)
767     return threshold *=
768            (noise_level == kDenLow) ? 2 : (temporal_layer_id == 2) ? 10 : 6;
769   else
770     return threshold;
771 }
772 
vp9_denoiser_reset_on_first_frame(VP9_COMP * const cpi)773 void vp9_denoiser_reset_on_first_frame(VP9_COMP *const cpi) {
774   if (vp9_denoise_svc_non_key(cpi) &&
775       cpi->denoiser.current_denoiser_frame == 0) {
776     cpi->denoiser.reset = 1;
777     force_refresh_longterm_ref(cpi);
778   }
779 }
780 
vp9_denoiser_update_ref_frame(VP9_COMP * const cpi)781 void vp9_denoiser_update_ref_frame(VP9_COMP *const cpi) {
782   VP9_COMMON *const cm = &cpi->common;
783   SVC *const svc = &cpi->svc;
784 
785   if (cpi->oxcf.noise_sensitivity > 0 && denoise_svc(cpi) &&
786       cpi->denoiser.denoising_level > kDenLowLow) {
787     int svc_refresh_denoiser_buffers = 0;
788     int denoise_svc_second_layer = 0;
789     FRAME_TYPE frame_type = cm->intra_only ? KEY_FRAME : cm->frame_type;
790     cpi->denoiser.current_denoiser_frame++;
791     if (cpi->use_svc) {
792       const int svc_buf_shift =
793           svc->number_spatial_layers - svc->spatial_layer_id == 2
794               ? cpi->denoiser.num_ref_frames
795               : 0;
796       int layer =
797           LAYER_IDS_TO_IDX(svc->spatial_layer_id, svc->temporal_layer_id,
798                            svc->number_temporal_layers);
799       LAYER_CONTEXT *const lc = &svc->layer_context[layer];
800       svc_refresh_denoiser_buffers =
801           lc->is_key_frame || svc->spatial_layer_sync[svc->spatial_layer_id];
802       denoise_svc_second_layer =
803           svc->number_spatial_layers - svc->spatial_layer_id == 2 ? 1 : 0;
804       // Check if we need to allocate extra buffers in the denoiser
805       // for refreshed frames.
806       if (vp9_denoiser_realloc_svc(cm, &cpi->denoiser, svc, svc_buf_shift,
807                                    cpi->refresh_alt_ref_frame,
808                                    cpi->refresh_golden_frame,
809                                    cpi->refresh_last_frame, cpi->alt_fb_idx,
810                                    cpi->gld_fb_idx, cpi->lst_fb_idx))
811         vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
812                            "Failed to re-allocate denoiser for SVC");
813     }
814     vp9_denoiser_update_frame_info(
815         &cpi->denoiser, *cpi->Source, svc, frame_type,
816         cpi->refresh_alt_ref_frame, cpi->refresh_golden_frame,
817         cpi->refresh_last_frame, cpi->alt_fb_idx, cpi->gld_fb_idx,
818         cpi->lst_fb_idx, cpi->resize_pending, svc_refresh_denoiser_buffers,
819         denoise_svc_second_layer);
820   }
821 }
822 
823 #ifdef OUTPUT_YUV_DENOISED
make_grayscale(YV12_BUFFER_CONFIG * yuv)824 static void make_grayscale(YV12_BUFFER_CONFIG *yuv) {
825   int r, c;
826   uint8_t *u = yuv->u_buffer;
827   uint8_t *v = yuv->v_buffer;
828 
829   for (r = 0; r < yuv->uv_height; ++r) {
830     for (c = 0; c < yuv->uv_width; ++c) {
831       u[c] = UINT8_MAX / 2;
832       v[c] = UINT8_MAX / 2;
833     }
834     u += yuv->uv_stride;
835     v += yuv->uv_stride;
836   }
837 }
838 #endif
839