vp9_denoiser.c 19.8 KB
Newer Older
Tim Kopp's avatar
Tim Kopp committed
1 2 3 4 5 6 7 8 9 10
/*
 *  Copyright (c) 2012 The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

Tim Kopp's avatar
Tim Kopp committed
11
#include <assert.h>
12
#include <limits.h>
13 14
#include <math.h>

Zoe Liu's avatar
Zoe Liu committed
15
#include "./vpx_dsp_rtcd.h"
16
#include "vpx_dsp/vpx_dsp_common.h"
Tim Kopp's avatar
Tim Kopp committed
17
#include "vpx_scale/yv12config.h"
Tim Kopp's avatar
Tim Kopp committed
18
#include "vpx/vpx_integer.h"
19
#include "vp9/common/vp9_reconinter.h"
20
#include "vp9/encoder/vp9_context_tree.h"
Tim Kopp's avatar
Tim Kopp committed
21
#include "vp9/encoder/vp9_denoiser.h"
22
#include "vp9/encoder/vp9_encoder.h"
Tim Kopp's avatar
Tim Kopp committed
23

Tim Kopp's avatar
Tim Kopp committed
24 25 26 27
#ifdef OUTPUT_YUV_DENOISED
static void make_grayscale(YV12_BUFFER_CONFIG *yuv);
#endif

28 29 30 31 32 33 34 35 36 37 38 39 40 41
static int absdiff_thresh(BLOCK_SIZE bs, int increase_denoising) {
  (void)bs;
  return 3 + (increase_denoising ? 1 : 0);
}

static int delta_thresh(BLOCK_SIZE bs, int increase_denoising) {
  (void)bs;
  (void)increase_denoising;
  return 4;
}

static int noise_motion_thresh(BLOCK_SIZE bs, int increase_denoising) {
  (void)bs;
  (void)increase_denoising;
42
  return 625;
43 44 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 49
}

static int sse_diff_thresh(BLOCK_SIZE bs, int increase_denoising,
50
                           int motion_magnitude) {
clang-format's avatar
clang-format committed
51
  if (motion_magnitude > noise_motion_thresh(bs, increase_denoising)) {
52 53 54 55
    if (increase_denoising)
      return (1 << num_pels_log2_lookup[bs]) << 2;
    else
      return 0;
56
  } else {
57
    return (1 << num_pels_log2_lookup[bs]) << 4;
58 59 60 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 66 67 68
// TODO(jackychen): If increase_denoising is enabled in the future,
// we might need to update the code for calculating 'total_adj' in
// case the C code is not bit-exact with corresponding sse2 code.
int vp9_denoiser_filter_c(const uint8_t *sig, int sig_stride,
clang-format's avatar
clang-format committed
69 70 71
                          const uint8_t *mc_avg, int mc_avg_stride,
                          uint8_t *avg, int avg_stride, int increase_denoising,
                          BLOCK_SIZE bs, int motion_magnitude) {
Tim Kopp's avatar
Tim Kopp committed
72
  int r, c;
73 74 75 76
  const uint8_t *sig_start = sig;
  const uint8_t *mc_avg_start = mc_avg;
  uint8_t *avg_start = avg;
  int diff, adj, absdiff, delta;
clang-format's avatar
clang-format committed
77
  int adj_val[] = { 3, 4, 6 };
Tim Kopp's avatar
Tim Kopp committed
78
  int total_adj = 0;
79 80
  int shift_inc = 1;

81 82 83
  // If motion_magnitude is small, making the denoiser more aggressive by
  // increasing the adjustment for each level. Add another increment for
  // blocks that are labeled for increase denoising.
84 85 86 87 88 89 90 91
  if (motion_magnitude <= MOTION_MAGNITUDE_THRESHOLD) {
    if (increase_denoising) {
      shift_inc = 2;
    }
    adj_val[0] += shift_inc;
    adj_val[1] += shift_inc;
    adj_val[2] += shift_inc;
  }
Tim Kopp's avatar
Tim Kopp committed
92

93
  // First attempt to apply a strong temporal denoising filter.
94 95
  for (r = 0; r < (4 << b_height_log2_lookup[bs]); ++r) {
    for (c = 0; c < (4 << b_width_log2_lookup[bs]); ++c) {
Tim Kopp's avatar
Tim Kopp committed
96
      diff = mc_avg[c] - sig[c];
97
      absdiff = abs(diff);
Tim Kopp's avatar
Tim Kopp committed
98

99
      if (absdiff <= absdiff_thresh(bs, increase_denoising)) {
100 101
        avg[c] = mc_avg[c];
        total_adj += diff;
Tim Kopp's avatar
Tim Kopp committed
102
      } else {
103
        switch (absdiff) {
clang-format's avatar
clang-format committed
104 105 106 107 108 109 110 111 112 113 114 115 116
          case 4:
          case 5:
          case 6:
          case 7: adj = adj_val[0]; break;
          case 8:
          case 9:
          case 10:
          case 11:
          case 12:
          case 13:
          case 14:
          case 15: adj = adj_val[1]; break;
          default: adj = adj_val[2];
117 118
        }
        if (diff > 0) {
119
          avg[c] = VPXMIN(UINT8_MAX, sig[c] + adj);
120 121
          total_adj += adj;
        } else {
122
          avg[c] = VPXMAX(0, sig[c] - adj);
123 124
          total_adj -= adj;
        }
Tim Kopp's avatar
Tim Kopp committed
125 126 127 128 129 130
      }
    }
    sig += sig_stride;
    avg += avg_stride;
    mc_avg += mc_avg_stride;
  }
131 132 133 134 135 136 137

  // If the strong filter did not modify the signal too much, we're all set.
  if (abs(total_adj) <= total_adj_strong_thresh(bs, increase_denoising)) {
    return FILTER_BLOCK;
  }

  // Otherwise, we try to dampen the filter if the delta is not too high.
clang-format's avatar
clang-format committed
138 139 140
  delta = ((abs(total_adj) - total_adj_strong_thresh(bs, increase_denoising)) >>
           num_pels_log2_lookup[bs]) +
          1;
141 142

  if (delta >= delta_thresh(bs, increase_denoising)) {
143 144 145
    return COPY_BLOCK;
  }

clang-format's avatar
clang-format committed
146
  mc_avg = mc_avg_start;
147 148
  avg = avg_start;
  sig = sig_start;
149 150
  for (r = 0; r < (4 << b_height_log2_lookup[bs]); ++r) {
    for (c = 0; c < (4 << b_width_log2_lookup[bs]); ++c) {
151 152 153 154 155 156
      diff = mc_avg[c] - sig[c];
      adj = abs(diff);
      if (adj > delta) {
        adj = delta;
      }
      if (diff > 0) {
JackyChen's avatar
JackyChen committed
157 158 159
        // Diff positive means we made positive adjustment above
        // (in first try/attempt), so now make negative adjustment to bring
        // denoised signal down.
160
        avg[c] = VPXMAX(0, avg[c] - adj);
JackyChen's avatar
JackyChen committed
161
        total_adj -= adj;
162
      } else {
JackyChen's avatar
JackyChen committed
163 164 165
        // Diff negative means we made negative adjustment above
        // (in first try/attempt), so now make positive adjustment to bring
        // denoised signal up.
166
        avg[c] = VPXMIN(UINT8_MAX, avg[c] + adj);
JackyChen's avatar
JackyChen committed
167
        total_adj += adj;
168 169 170 171 172 173 174 175 176 177 178 179
      }
    }
    sig += sig_stride;
    avg += avg_stride;
    mc_avg += mc_avg_stride;
  }

  // We can use the filter if it has been sufficiently dampened
  if (abs(total_adj) <= total_adj_weak_thresh(bs, increase_denoising)) {
    return FILTER_BLOCK;
  }
  return COPY_BLOCK;
Tim Kopp's avatar
Tim Kopp committed
180 181
}

clang-format's avatar
clang-format committed
182 183 184
static uint8_t *block_start(uint8_t *framebuf, int stride, int mi_row,
                            int mi_col) {
  return framebuf + (stride * mi_row << 3) + (mi_col << 3);
185 186
}

clang-format's avatar
clang-format committed
187 188 189
static VP9_DENOISER_DECISION perform_motion_compensation(
    VP9_DENOISER *denoiser, MACROBLOCK *mb, BLOCK_SIZE bs,
    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) {
192
  int sse_diff = ctx->zeromv_sse - ctx->newmv_sse;
193 194
  MV_REFERENCE_FRAME frame;
  MACROBLOCKD *filter_mbd = &mb->e_mbd;
Scott LaVarnway's avatar
Scott LaVarnway committed
195 196
  MODE_INFO *mi = filter_mbd->mi[0];
  MODE_INFO saved_mi;
Marco's avatar
Marco committed
197
  int i;
Tim Kopp's avatar
Tim Kopp committed
198
  struct buf_2d saved_dst[MAX_MB_PLANE];
Marco's avatar
Marco committed
199
  struct buf_2d saved_pre[MAX_MB_PLANE];
Tim Kopp's avatar
Tim Kopp committed
200

201
  frame = ctx->best_reference_frame;
Scott LaVarnway's avatar
Scott LaVarnway committed
202
  saved_mi = *mi;
203

clang-format's avatar
clang-format committed
204
  if (is_skin && (motion_magnitude > 0 || consec_zeromv < 4)) return COPY_BLOCK;
205

206 207 208 209 210
  // Avoid denoising small blocks. When noise > kDenLow or frame width > 480,
  // denoise 16x16 blocks.
  if (bs == BLOCK_8X8 || bs == BLOCK_8X16 || bs == BLOCK_16X8 ||
      (bs == BLOCK_16X16 && width > 480 &&
       denoiser->denoising_level <= kDenLow))
211 212
    return COPY_BLOCK;

213 214
  // If the best reference frame uses inter-prediction and there is enough of a
  // difference in sum-squared-error, use it.
215 216 217
  if (frame != INTRA_FRAME &&
      (frame != GOLDEN_FRAME || num_spatial_layers == 1) &&
      ctx->newmv_sse != UINT_MAX &&
Marco's avatar
Marco committed
218
      sse_diff > sse_diff_thresh(bs, increase_denoising, motion_magnitude)) {
Scott LaVarnway's avatar
Scott LaVarnway committed
219 220 221
    mi->ref_frame[0] = ctx->best_reference_frame;
    mi->mode = ctx->best_sse_inter_mode;
    mi->mv[0] = ctx->best_sse_mv;
222 223
  } else {
    // Otherwise, use the zero reference frame.
224
    frame = ctx->best_zeromv_reference_frame;
225 226
    ctx->newmv_sse = ctx->zeromv_sse;
    // Bias to last reference.
227 228 229 230
    if (num_spatial_layers > 1 ||
        (frame != LAST_FRAME &&
         ((ctx->zeromv_lastref_sse<(5 * ctx->zeromv_sse)>> 2) ||
          denoiser->denoising_level >= kDenHigh))) {
231 232 233
      frame = LAST_FRAME;
      ctx->newmv_sse = ctx->zeromv_lastref_sse;
    }
Scott LaVarnway's avatar
Scott LaVarnway committed
234 235 236
    mi->ref_frame[0] = frame;
    mi->mode = ZEROMV;
    mi->mv[0].as_int = 0;
237 238
    ctx->best_sse_inter_mode = ZEROMV;
    ctx->best_sse_mv.as_int = 0;
239
    *zeromv_filter = 1;
240 241 242
    if (denoiser->denoising_level > kDenMedium) {
      motion_magnitude = 0;
    }
243 244
  }

245 246
  if (ctx->newmv_sse > sse_thresh(bs, increase_denoising)) {
    // Restore everything to its original state
Scott LaVarnway's avatar
Scott LaVarnway committed
247
    *mi = saved_mi;
248 249
    return COPY_BLOCK;
  }
clang-format's avatar
clang-format committed
250
  if (motion_magnitude > (noise_motion_thresh(bs, increase_denoising) << 3)) {
251
    // Restore everything to its original state
Scott LaVarnway's avatar
Scott LaVarnway committed
252
    *mi = saved_mi;
253 254 255 256 257
    return COPY_BLOCK;
  }

  // We will restore these after motion compensation.
  for (i = 0; i < MAX_MB_PLANE; ++i) {
Marco's avatar
Marco committed
258
    saved_pre[i] = filter_mbd->plane[i].pre[0];
259 260 261
    saved_dst[i] = filter_mbd->plane[i].dst;
  }

262 263
  // Set the pointers in the MACROBLOCKD to point to the buffers in the denoiser
  // struct.
Marco's avatar
Marco committed
264 265
  filter_mbd->plane[0].pre[0].buf =
      block_start(denoiser->running_avg_y[frame].y_buffer,
clang-format's avatar
clang-format committed
266 267
                  denoiser->running_avg_y[frame].y_stride, mi_row, mi_col);
  filter_mbd->plane[0].pre[0].stride = denoiser->running_avg_y[frame].y_stride;
Marco's avatar
Marco committed
268
  filter_mbd->plane[1].pre[0].buf =
clang-format's avatar
clang-format committed
269 270 271
      block_start(denoiser->running_avg_y[frame].u_buffer,
                  denoiser->running_avg_y[frame].uv_stride, mi_row, mi_col);
  filter_mbd->plane[1].pre[0].stride = denoiser->running_avg_y[frame].uv_stride;
Marco's avatar
Marco committed
272 273
  filter_mbd->plane[2].pre[0].buf =
      block_start(denoiser->running_avg_y[frame].v_buffer,
clang-format's avatar
clang-format committed
274 275
                  denoiser->running_avg_y[frame].uv_stride, mi_row, mi_col);
  filter_mbd->plane[2].pre[0].stride = denoiser->running_avg_y[frame].uv_stride;
Marco's avatar
Marco committed
276

277 278
  filter_mbd->plane[0].dst.buf =
      block_start(denoiser->mc_running_avg_y.y_buffer,
clang-format's avatar
clang-format committed
279
                  denoiser->mc_running_avg_y.y_stride, mi_row, mi_col);
280 281 282
  filter_mbd->plane[0].dst.stride = denoiser->mc_running_avg_y.y_stride;
  filter_mbd->plane[1].dst.buf =
      block_start(denoiser->mc_running_avg_y.u_buffer,
clang-format's avatar
clang-format committed
283
                  denoiser->mc_running_avg_y.uv_stride, mi_row, mi_col);
Tim Kopp's avatar
Tim Kopp committed
284
  filter_mbd->plane[1].dst.stride = denoiser->mc_running_avg_y.uv_stride;
285 286
  filter_mbd->plane[2].dst.buf =
      block_start(denoiser->mc_running_avg_y.v_buffer,
clang-format's avatar
clang-format committed
287
                  denoiser->mc_running_avg_y.uv_stride, mi_row, mi_col);
Tim Kopp's avatar
Tim Kopp committed
288
  filter_mbd->plane[2].dst.stride = denoiser->mc_running_avg_y.uv_stride;
289

Marco's avatar
Marco committed
290
  vp9_build_inter_predictors_sby(filter_mbd, mi_row, mi_col, bs);
291 292

  // Restore everything to its original state
Scott LaVarnway's avatar
Scott LaVarnway committed
293
  *mi = saved_mi;
Tim Kopp's avatar
Tim Kopp committed
294
  for (i = 0; i < MAX_MB_PLANE; ++i) {
Marco's avatar
Marco committed
295
    filter_mbd->plane[i].pre[0] = saved_pre[i];
Tim Kopp's avatar
Tim Kopp committed
296 297
    filter_mbd->plane[i].dst = saved_dst[i];
  }
298 299 300 301

  return FILTER_BLOCK;
}

clang-format's avatar
clang-format committed
302 303
void vp9_denoiser_denoise(VP9_COMP *cpi, MACROBLOCK *mb, int mi_row, int mi_col,
                          BLOCK_SIZE bs, PICK_MODE_CONTEXT *ctx,
304
                          VP9_DENOISER_DECISION *denoiser_decision) {
Marco's avatar
Marco committed
305
  int mv_col, mv_row;
306
  int motion_magnitude = 0;
307
  int zeromv_filter = 0;
308
  VP9_DENOISER *denoiser = &cpi->denoiser;
309
  VP9_DENOISER_DECISION decision = COPY_BLOCK;
310
  YV12_BUFFER_CONFIG avg = denoiser->running_avg_y[INTRA_FRAME];
Tim Kopp's avatar
Tim Kopp committed
311 312
  YV12_BUFFER_CONFIG mc_avg = denoiser->mc_running_avg_y;
  uint8_t *avg_start = block_start(avg.y_buffer, avg.y_stride, mi_row, mi_col);
clang-format's avatar
clang-format committed
313 314
  uint8_t *mc_avg_start =
      block_start(mc_avg.y_buffer, mc_avg.y_stride, mi_row, mi_col);
315
  struct buf_2d src = mb->plane[0].src;
316
  int is_skin = 0;
317
  int consec_zeromv = 0;
318 319 320
  mv_col = ctx->best_sse_mv.as_mv.col;
  mv_row = ctx->best_sse_mv.as_mv.row;
  motion_magnitude = mv_row * mv_row + mv_col * mv_col;
321

clang-format's avatar
clang-format committed
322
  if (cpi->use_skin_detection && bs <= BLOCK_32X32 &&
323
      denoiser->denoising_level < kDenHigh) {
324 325 326 327
    int motion_level = (motion_magnitude < 16) ? 0 : 1;
    // If motion for current block is small/zero, compute consec_zeromv for
    // skin detection (early exit in skin detection is done for large
    // consec_zeromv when current block has small/zero motion).
328
    consec_zeromv = 0;
329
    if (motion_level == 0) {
clang-format's avatar
clang-format committed
330
      VP9_COMMON *const cm = &cpi->common;
331 332 333 334 335 336 337 338 339 340 341
      int j, i;
      // Loop through the 8x8 sub-blocks.
      const int bw = num_8x8_blocks_wide_lookup[BLOCK_64X64];
      const int bh = num_8x8_blocks_high_lookup[BLOCK_64X64];
      const int xmis = VPXMIN(cm->mi_cols - mi_col, bw);
      const int ymis = VPXMIN(cm->mi_rows - mi_row, bh);
      const int block_index = mi_row * cm->mi_cols + mi_col;
      consec_zeromv = 100;
      for (i = 0; i < ymis; i++) {
        for (j = 0; j < xmis; j++) {
          int bl_index = block_index + i * cm->mi_cols + j;
342
          consec_zeromv = VPXMIN(cpi->consec_zero_mv[bl_index], consec_zeromv);
343 344
          // No need to keep checking 8x8 blocks if any of the sub-blocks
          // has small consec_zeromv (since threshold for no_skin based on
345 346
          // zero/small motion in skin detection is high, i.e, > 4).
          if (consec_zeromv < 4) {
347 348 349 350 351 352 353
            i = ymis;
            j = xmis;
          }
        }
      }
    }
    // TODO(marpan): Compute skin detection over sub-blocks.
clang-format's avatar
clang-format committed
354 355 356 357
    is_skin = vp9_compute_skin_block(
        mb->plane[0].src.buf, mb->plane[1].src.buf, mb->plane[2].src.buf,
        mb->plane[0].src.stride, mb->plane[1].src.stride, bs, consec_zeromv,
        motion_level);
358
  }
clang-format's avatar
clang-format committed
359
  if (!is_skin && denoiser->denoising_level == kDenHigh) {
Marco's avatar
Marco committed
360 361 362 363 364
    denoiser->increase_denoising = 1;
  } else {
    denoiser->increase_denoising = 0;
  }

365
  if (denoiser->denoising_level >= kDenLow)
clang-format's avatar
clang-format committed
366 367
    decision = perform_motion_compensation(
        denoiser, mb, bs, denoiser->increase_denoising, mi_row, mi_col, ctx,
368
        motion_magnitude, is_skin, &zeromv_filter, consec_zeromv,
369
        cpi->svc.number_spatial_layers, cpi->Source->y_width);
370 371

  if (decision == FILTER_BLOCK) {
clang-format's avatar
clang-format committed
372 373 374
    decision = vp9_denoiser_filter(
        src.buf, src.stride, mc_avg_start, mc_avg.y_stride, avg_start,
        avg.y_stride, denoiser->increase_denoising, bs, motion_magnitude);
375
  }
376 377

  if (decision == FILTER_BLOCK) {
clang-format's avatar
clang-format committed
378 379
    vpx_convolve_copy(avg_start, avg.y_stride, src.buf, src.stride, NULL, 0,
                      NULL, 0, num_4x4_blocks_wide_lookup[bs] << 2,
380
                      num_4x4_blocks_high_lookup[bs] << 2);
381
  } else {  // COPY_BLOCK
clang-format's avatar
clang-format committed
382 383
    vpx_convolve_copy(src.buf, src.stride, avg_start, avg.y_stride, NULL, 0,
                      NULL, 0, num_4x4_blocks_wide_lookup[bs] << 2,
384
                      num_4x4_blocks_high_lookup[bs] << 2);
385
  }
386
  *denoiser_decision = decision;
387 388
  if (decision == FILTER_BLOCK && zeromv_filter == 1)
    *denoiser_decision = FILTER_ZEROMV_BLOCK;
Tim Kopp's avatar
Tim Kopp committed
389 390
}

clang-format's avatar
clang-format committed
391 392
static void copy_frame(YV12_BUFFER_CONFIG *const dest,
                       const YV12_BUFFER_CONFIG *const src) {
Tim Kopp's avatar
Tim Kopp committed
393
  int r;
394 395
  const uint8_t *srcbuf = src->y_buffer;
  uint8_t *destbuf = dest->y_buffer;
396

397 398
  assert(dest->y_width == src->y_width);
  assert(dest->y_height == src->y_height);
399

400 401 402 403
  for (r = 0; r < dest->y_height; ++r) {
    memcpy(destbuf, srcbuf, dest->y_width);
    destbuf += dest->y_stride;
    srcbuf += src->y_stride;
404
  }
405 406
}

clang-format's avatar
clang-format committed
407 408
static void swap_frame_buffer(YV12_BUFFER_CONFIG *const dest,
                              YV12_BUFFER_CONFIG *const src) {
409 410 411 412 413
  uint8_t *tmp_buf = dest->y_buffer;
  assert(dest->y_width == src->y_width);
  assert(dest->y_height == src->y_height);
  dest->y_buffer = src->y_buffer;
  src->y_buffer = tmp_buf;
414 415
}

Tim Kopp's avatar
Tim Kopp committed
416
void vp9_denoiser_update_frame_info(VP9_DENOISER *denoiser,
417
                                    YV12_BUFFER_CONFIG src,
Tim Kopp's avatar
Tim Kopp committed
418 419 420
                                    FRAME_TYPE frame_type,
                                    int refresh_alt_ref_frame,
                                    int refresh_golden_frame,
clang-format's avatar
clang-format committed
421
                                    int refresh_last_frame, int resized) {
Marco's avatar
Marco committed
422 423
  // Copy source into denoised reference buffers on KEY_FRAME or
  // if the just encoded frame was resized.
424
  if (frame_type == KEY_FRAME || resized != 0 || denoiser->reset) {
Tim Kopp's avatar
Tim Kopp committed
425
    int i;
426
    // Start at 1 so as not to overwrite the INTRA_FRAME
427
    for (i = 1; i < MAX_REF_FRAMES; ++i)
428
      copy_frame(&denoiser->running_avg_y[i], &src);
429
    denoiser->reset = 0;
430 431 432
    return;
  }

433
  // If more than one refresh occurs, must copy frame buffer.
clang-format's avatar
clang-format committed
434
  if ((refresh_alt_ref_frame + refresh_golden_frame + refresh_last_frame) > 1) {
435
    if (refresh_alt_ref_frame) {
436 437
      copy_frame(&denoiser->running_avg_y[ALTREF_FRAME],
                 &denoiser->running_avg_y[INTRA_FRAME]);
438 439
    }
    if (refresh_golden_frame) {
440 441
      copy_frame(&denoiser->running_avg_y[GOLDEN_FRAME],
                 &denoiser->running_avg_y[INTRA_FRAME]);
442 443
    }
    if (refresh_last_frame) {
444 445
      copy_frame(&denoiser->running_avg_y[LAST_FRAME],
                 &denoiser->running_avg_y[INTRA_FRAME]);
446 447 448 449 450 451 452 453 454 455 456 457 458 459
    }
  } else {
    if (refresh_alt_ref_frame) {
      swap_frame_buffer(&denoiser->running_avg_y[ALTREF_FRAME],
                        &denoiser->running_avg_y[INTRA_FRAME]);
    }
    if (refresh_golden_frame) {
      swap_frame_buffer(&denoiser->running_avg_y[GOLDEN_FRAME],
                        &denoiser->running_avg_y[INTRA_FRAME]);
    }
    if (refresh_last_frame) {
      swap_frame_buffer(&denoiser->running_avg_y[LAST_FRAME],
                        &denoiser->running_avg_y[INTRA_FRAME]);
    }
460
  }
Tim Kopp's avatar
Tim Kopp committed
461 462
}

463 464
void vp9_denoiser_reset_frame_stats(PICK_MODE_CONTEXT *ctx) {
  ctx->zeromv_sse = UINT_MAX;
Marco's avatar
Marco committed
465
  ctx->newmv_sse = UINT_MAX;
466
  ctx->zeromv_lastref_sse = UINT_MAX;
467
  ctx->best_sse_mv.as_int = 0;
468 469
}

Scott LaVarnway's avatar
Scott LaVarnway committed
470
void vp9_denoiser_update_frame_stats(MODE_INFO *mi, unsigned int sse,
471
                                     PREDICTION_MODE mode,
472
                                     PICK_MODE_CONTEXT *ctx) {
Scott LaVarnway's avatar
Scott LaVarnway committed
473
  if (mi->mv[0].as_int == 0 && sse < ctx->zeromv_sse) {
474
    ctx->zeromv_sse = sse;
Scott LaVarnway's avatar
Scott LaVarnway committed
475
    ctx->best_zeromv_reference_frame = mi->ref_frame[0];
clang-format's avatar
clang-format committed
476
    if (mi->ref_frame[0] == LAST_FRAME) ctx->zeromv_lastref_sse = sse;
477 478
  }

Scott LaVarnway's avatar
Scott LaVarnway committed
479
  if (mi->mv[0].as_int != 0 && sse < ctx->newmv_sse) {
480 481
    ctx->newmv_sse = sse;
    ctx->best_sse_inter_mode = mode;
Scott LaVarnway's avatar
Scott LaVarnway committed
482 483
    ctx->best_sse_mv = mi->mv[0];
    ctx->best_reference_frame = mi->ref_frame[0];
484
  }
Tim Kopp's avatar
Tim Kopp committed
485 486
}

clang-format's avatar
clang-format committed
487 488
int vp9_denoiser_alloc(VP9_DENOISER *denoiser, int width, int height, int ssx,
                       int ssy,
489 490 491 492
#if CONFIG_VP9_HIGHBITDEPTH
                       int use_highbitdepth,
#endif
                       int border) {
493
  int i, fail;
494
  const int legacy_byte_alignment = 0;
Tim Kopp's avatar
Tim Kopp committed
495
  assert(denoiser != NULL);
496

497
  for (i = 0; i < MAX_REF_FRAMES; ++i) {
498
    fail = vpx_alloc_frame_buffer(&denoiser->running_avg_y[i], width, height,
499 500 501 502
                                  ssx, ssy,
#if CONFIG_VP9_HIGHBITDEPTH
                                  use_highbitdepth,
#endif
503
                                  border, legacy_byte_alignment);
504 505 506 507
    if (fail) {
      vp9_denoiser_free(denoiser);
      return 1;
    }
Tim Kopp's avatar
Tim Kopp committed
508 509 510
#ifdef OUTPUT_YUV_DENOISED
    make_grayscale(&denoiser->running_avg_y[i]);
#endif
511 512
  }

clang-format's avatar
clang-format committed
513 514
  fail = vpx_alloc_frame_buffer(&denoiser->mc_running_avg_y, width, height, ssx,
                                ssy,
515 516
#if CONFIG_VP9_HIGHBITDEPTH
                                use_highbitdepth,
517 518 519 520 521 522 523
#endif
                                border, legacy_byte_alignment);
  if (fail) {
    vp9_denoiser_free(denoiser);
    return 1;
  }

clang-format's avatar
clang-format committed
524
  fail = vpx_alloc_frame_buffer(&denoiser->last_source, width, height, ssx, ssy,
525 526
#if CONFIG_VP9_HIGHBITDEPTH
                                use_highbitdepth,
527
#endif
528
                                border, legacy_byte_alignment);
529
  if (fail) {
530 531 532
    vp9_denoiser_free(denoiser);
    return 1;
  }
Tim Kopp's avatar
Tim Kopp committed
533 534 535
#ifdef OUTPUT_YUV_DENOISED
  make_grayscale(&denoiser->running_avg_y[i]);
#endif
536
  denoiser->increase_denoising = 0;
537
  denoiser->frame_buffer_initialized = 1;
538
  denoiser->denoising_level = kDenLow;
539 540
  denoiser->prev_denoising_level = kDenLow;
  denoiser->reset = 0;
Tim Kopp's avatar
Tim Kopp committed
541 542 543 544
  return 0;
}

void vp9_denoiser_free(VP9_DENOISER *denoiser) {
545
  int i;
Tim Kopp's avatar
Tim Kopp committed
546 547 548
  if (denoiser == NULL) {
    return;
  }
549
  denoiser->frame_buffer_initialized = 0;
550
  for (i = 0; i < MAX_REF_FRAMES; ++i) {
551
    vpx_free_frame_buffer(&denoiser->running_avg_y[i]);
552
  }
553
  vpx_free_frame_buffer(&denoiser->mc_running_avg_y);
554 555 556
  vpx_free_frame_buffer(&denoiser->last_source);
}

clang-format's avatar
clang-format committed
557
void vp9_denoiser_set_noise_level(VP9_DENOISER *denoiser, int noise_level) {
558
  denoiser->denoising_level = noise_level;
559 560 561 562 563 564
  if (denoiser->denoising_level > kDenLowLow &&
      denoiser->prev_denoising_level == kDenLowLow)
    denoiser->reset = 1;
  else
    denoiser->reset = 0;
  denoiser->prev_denoising_level = denoiser->denoising_level;
Tim Kopp's avatar
Tim Kopp committed
565
}
Tim Kopp's avatar
Tim Kopp committed
566 567 568 569 570 571 572

#ifdef OUTPUT_YUV_DENOISED
static void make_grayscale(YV12_BUFFER_CONFIG *yuv) {
  int r, c;
  uint8_t *u = yuv->u_buffer;
  uint8_t *v = yuv->v_buffer;

573 574
  for (r = 0; r < yuv->uv_height; ++r) {
    for (c = 0; c < yuv->uv_width; ++c) {
Tim Kopp's avatar
Tim Kopp committed
575 576 577
      u[c] = UINT8_MAX / 2;
      v[c] = UINT8_MAX / 2;
    }
578 579
    u += yuv->uv_stride;
    v += yuv->uv_stride;
Tim Kopp's avatar
Tim Kopp committed
580 581 582
  }
}
#endif