vp9_denoiser.c 19.3 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

Marco's avatar
Marco committed
24
/* The VP9 denoiser is similar to that of the VP8 denoiser. While
25 26 27 28 29
 * choosing the motion vectors / reference frames, the denoiser is run, and if
 * it did not modify the signal to much, the denoised block is copied to the
 * signal.
 */

Tim Kopp's avatar
Tim Kopp committed
30 31 32 33
#ifdef OUTPUT_YUV_DENOISED
static void make_grayscale(YV12_BUFFER_CONFIG *yuv);
#endif

34 35 36 37 38 39 40 41 42 43 44 45 46 47
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;
48
  return 625;
49 50 51
}

static unsigned int sse_thresh(BLOCK_SIZE bs, int increase_denoising) {
52
  return (1 << num_pels_log2_lookup[bs]) * (increase_denoising ? 60 : 40);
53 54 55
}

static int sse_diff_thresh(BLOCK_SIZE bs, int increase_denoising,
56 57
                           int motion_magnitude) {
  if (motion_magnitude >
58 59 60
      noise_motion_thresh(bs, increase_denoising)) {
    return 0;
  } else {
61
    return (1 << num_pels_log2_lookup[bs]) * 20;
62 63 64 65
  }
}

static int total_adj_weak_thresh(BLOCK_SIZE bs, int increase_denoising) {
66
  return (1 << num_pels_log2_lookup[bs]) * (increase_denoising ? 3 : 2);
67 68
}

69 70 71 72 73 74 75 76 77 78
// 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,
                          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
79
  int r, c;
80 81 82 83
  const uint8_t *sig_start = sig;
  const uint8_t *mc_avg_start = mc_avg;
  uint8_t *avg_start = avg;
  int diff, adj, absdiff, delta;
84
  int adj_val[] = {3, 4, 6};
Tim Kopp's avatar
Tim Kopp committed
85
  int total_adj = 0;
86 87
  int shift_inc = 1;

88 89 90
  // 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.
91 92 93 94 95 96 97 98
  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
99

100
  // First attempt to apply a strong temporal denoising filter.
101 102
  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
103
      diff = mc_avg[c] - sig[c];
104
      absdiff = abs(diff);
Tim Kopp's avatar
Tim Kopp committed
105

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

  // 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.
  delta = ((abs(total_adj) - total_adj_strong_thresh(bs, increase_denoising))
142
           >> num_pels_log2_lookup[bs]) + 1;
143 144

  if (delta >= delta_thresh(bs, increase_denoising)) {
145 146 147 148 149 150
    return COPY_BLOCK;
  }

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

Tim Kopp's avatar
Tim Kopp committed
184 185
static uint8_t *block_start(uint8_t *framebuf, int stride,
                            int mi_row, int mi_col) {
186
  return framebuf + (stride * mi_row * 8) + (mi_col * 8);
187 188
}

189 190 191 192 193
static VP9_DENOISER_DECISION perform_motion_compensation(VP9_DENOISER *denoiser,
                                                         MACROBLOCK *mb,
                                                         BLOCK_SIZE bs,
                                                         int increase_denoising,
                                                         int mi_row,
194
                                                         int mi_col,
195
                                                         PICK_MODE_CONTEXT *ctx,
Marco's avatar
Marco committed
196
                                                         int motion_magnitude,
197 198
                                                         int is_skin,
                                                         int *zeromv_filter) {
199
  int mv_col, mv_row;
200
  int sse_diff = ctx->zeromv_sse - ctx->newmv_sse;
201 202
  MV_REFERENCE_FRAME frame;
  MACROBLOCKD *filter_mbd = &mb->e_mbd;
203
  MB_MODE_INFO *mbmi = &filter_mbd->mi[0]->mbmi;
Tim Kopp's avatar
Tim Kopp committed
204 205 206 207 208
  MB_MODE_INFO saved_mbmi;
  int i, j;
  struct buf_2d saved_dst[MAX_MB_PLANE];
  struct buf_2d saved_pre[MAX_MB_PLANE][2];  // 2 pre buffers

209 210 211
  mv_col = ctx->best_sse_mv.as_mv.col;
  mv_row = ctx->best_sse_mv.as_mv.row;
  frame = ctx->best_reference_frame;
212

213 214
  saved_mbmi = *mbmi;

215
  if (is_skin && motion_magnitude > 0)
216 217
    return COPY_BLOCK;

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

244 245 246 247 248
  if (ctx->newmv_sse > sse_thresh(bs, increase_denoising)) {
    // Restore everything to its original state
    *mbmi = saved_mbmi;
    return COPY_BLOCK;
  }
Marco's avatar
Marco committed
249
  if (motion_magnitude >
250
     (noise_motion_thresh(bs, increase_denoising) << 3)) {
251 252 253 254 255 256 257 258 259 260 261 262 263
    // Restore everything to its original state
    *mbmi = saved_mbmi;
    return COPY_BLOCK;
  }

  // We will restore these after motion compensation.
  for (i = 0; i < MAX_MB_PLANE; ++i) {
    for (j = 0; j < 2; ++j) {
      saved_pre[i][j] = filter_mbd->plane[i].pre[j];
    }
    saved_dst[i] = filter_mbd->plane[i].dst;
  }

264 265
  // Set the pointers in the MACROBLOCKD to point to the buffers in the denoiser
  // struct.
Tim Kopp's avatar
Tim Kopp committed
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
  for (j = 0; j < 2; ++j) {
    filter_mbd->plane[0].pre[j].buf =
        block_start(denoiser->running_avg_y[frame].y_buffer,
                    denoiser->running_avg_y[frame].y_stride,
                    mi_row, mi_col);
    filter_mbd->plane[0].pre[j].stride =
        denoiser->running_avg_y[frame].y_stride;
    filter_mbd->plane[1].pre[j].buf =
        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[j].stride =
        denoiser->running_avg_y[frame].uv_stride;
    filter_mbd->plane[2].pre[j].buf =
        block_start(denoiser->running_avg_y[frame].v_buffer,
                    denoiser->running_avg_y[frame].uv_stride,
                    mi_row, mi_col);
    filter_mbd->plane[2].pre[j].stride =
        denoiser->running_avg_y[frame].uv_stride;
  }
286 287 288 289 290 291 292 293 294
  filter_mbd->plane[0].dst.buf =
      block_start(denoiser->mc_running_avg_y.y_buffer,
                  denoiser->mc_running_avg_y.y_stride,
                  mi_row, mi_col);
  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,
                  denoiser->mc_running_avg_y.uv_stride,
                  mi_row, mi_col);
Tim Kopp's avatar
Tim Kopp committed
295
  filter_mbd->plane[1].dst.stride = denoiser->mc_running_avg_y.uv_stride;
296 297 298 299
  filter_mbd->plane[2].dst.buf =
      block_start(denoiser->mc_running_avg_y.v_buffer,
                  denoiser->mc_running_avg_y.uv_stride,
                  mi_row, mi_col);
Tim Kopp's avatar
Tim Kopp committed
300
  filter_mbd->plane[2].dst.stride = denoiser->mc_running_avg_y.uv_stride;
301 302 303 304 305

  vp9_build_inter_predictors_sby(filter_mbd, mv_row, mv_col, bs);

  // Restore everything to its original state
  *mbmi = saved_mbmi;
Tim Kopp's avatar
Tim Kopp committed
306 307 308 309 310 311
  for (i = 0; i < MAX_MB_PLANE; ++i) {
    for (j = 0; j < 2; ++j) {
      filter_mbd->plane[i].pre[j] = saved_pre[i][j];
    }
    filter_mbd->plane[i].dst = saved_dst[i];
  }
312

313 314
  mv_row = ctx->best_sse_mv.as_mv.row;
  mv_col = ctx->best_sse_mv.as_mv.col;
315 316 317 318

  return FILTER_BLOCK;
}

319
void vp9_denoiser_denoise(VP9_DENOISER *denoiser, MACROBLOCK *mb,
320
                          int mi_row, int mi_col, BLOCK_SIZE bs,
321 322
                          PICK_MODE_CONTEXT *ctx,
                          VP9_DENOISER_DECISION *denoiser_decision) {
Marco's avatar
Marco committed
323
  int mv_col, mv_row;
324
  int motion_magnitude = 0;
325
  int zeromv_filter = 0;
326
  VP9_DENOISER_DECISION decision = COPY_BLOCK;
327
  YV12_BUFFER_CONFIG avg = denoiser->running_avg_y[INTRA_FRAME];
Tim Kopp's avatar
Tim Kopp committed
328 329 330 331
  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);
  uint8_t *mc_avg_start = block_start(mc_avg.y_buffer, mc_avg.y_stride,
                                          mi_row, mi_col);
332
  struct buf_2d src = mb->plane[0].src;
333 334
  int is_skin = 0;

335
  if (bs <= BLOCK_16X16 && denoiser->denoising_level >= kDenLow) {
Marco's avatar
Marco committed
336 337 338 339 340 341
    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);
342
  }
343

Marco's avatar
Marco committed
344 345 346
  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;
347
  if (denoiser->denoising_level == kDenHigh && motion_magnitude < 16) {
Marco's avatar
Marco committed
348 349 350 351 352
    denoiser->increase_denoising = 1;
  } else {
    denoiser->increase_denoising = 0;
  }

353
  if (denoiser->denoising_level >= kDenLow)
354 355 356
    decision = perform_motion_compensation(denoiser, mb, bs,
                                           denoiser->increase_denoising,
                                           mi_row, mi_col, ctx,
Marco's avatar
Marco committed
357
                                           motion_magnitude,
358 359
                                           is_skin,
                                           &zeromv_filter);
360 361

  if (decision == FILTER_BLOCK) {
362 363 364
    decision = vp9_denoiser_filter(src.buf, src.stride,
                                 mc_avg_start, mc_avg.y_stride,
                                 avg_start, avg.y_stride,
Marco's avatar
Marco committed
365 366
                                 denoiser->increase_denoising,
                                 bs, motion_magnitude);
367
  }
368 369

  if (decision == FILTER_BLOCK) {
Zoe Liu's avatar
Zoe Liu committed
370
    vpx_convolve_copy(avg_start, avg.y_stride, src.buf, src.stride,
371 372 373
                      NULL, 0, NULL, 0,
                      num_4x4_blocks_wide_lookup[bs] << 2,
                      num_4x4_blocks_high_lookup[bs] << 2);
374
  } else {  // COPY_BLOCK
Zoe Liu's avatar
Zoe Liu committed
375
    vpx_convolve_copy(src.buf, src.stride, avg_start, avg.y_stride,
376 377 378
                      NULL, 0, NULL, 0,
                      num_4x4_blocks_wide_lookup[bs] << 2,
                      num_4x4_blocks_high_lookup[bs] << 2);
379
  }
380
  *denoiser_decision = decision;
381 382
  if (decision == FILTER_BLOCK && zeromv_filter == 1)
    *denoiser_decision = FILTER_ZEROMV_BLOCK;
Tim Kopp's avatar
Tim Kopp committed
383 384
}

385 386
static void copy_frame(YV12_BUFFER_CONFIG * const dest,
                       const YV12_BUFFER_CONFIG * const src) {
Tim Kopp's avatar
Tim Kopp committed
387
  int r;
388 389
  const uint8_t *srcbuf = src->y_buffer;
  uint8_t *destbuf = dest->y_buffer;
390

391 392
  assert(dest->y_width == src->y_width);
  assert(dest->y_height == src->y_height);
393

394 395 396 397
  for (r = 0; r < dest->y_height; ++r) {
    memcpy(destbuf, srcbuf, dest->y_width);
    destbuf += dest->y_stride;
    srcbuf += src->y_stride;
398
  }
399 400
}

401 402
static void swap_frame_buffer(YV12_BUFFER_CONFIG * const dest,
                              YV12_BUFFER_CONFIG * const src) {
403 404 405 406 407
  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;
408 409
}

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

427 428 429 430
  // If more than one refresh occurs, must copy frame buffer.
  if ((refresh_alt_ref_frame + refresh_golden_frame + refresh_last_frame)
      > 1) {
    if (refresh_alt_ref_frame) {
431 432
      copy_frame(&denoiser->running_avg_y[ALTREF_FRAME],
                 &denoiser->running_avg_y[INTRA_FRAME]);
433 434
    }
    if (refresh_golden_frame) {
435 436
      copy_frame(&denoiser->running_avg_y[GOLDEN_FRAME],
                 &denoiser->running_avg_y[INTRA_FRAME]);
437 438
    }
    if (refresh_last_frame) {
439 440
      copy_frame(&denoiser->running_avg_y[LAST_FRAME],
                 &denoiser->running_avg_y[INTRA_FRAME]);
441 442 443 444 445 446 447 448 449 450 451 452 453 454
    }
  } 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]);
    }
455
  }
Tim Kopp's avatar
Tim Kopp committed
456 457
}

458 459
void vp9_denoiser_reset_frame_stats(PICK_MODE_CONTEXT *ctx) {
  ctx->zeromv_sse = UINT_MAX;
Marco's avatar
Marco committed
460
  ctx->newmv_sse = UINT_MAX;
461
  ctx->zeromv_lastref_sse = UINT_MAX;
462 463
}

464 465
void vp9_denoiser_update_frame_stats(MB_MODE_INFO *mbmi, unsigned int sse,
                                     PREDICTION_MODE mode,
466
                                     PICK_MODE_CONTEXT *ctx) {
467
  // TODO(tkopp): Use both MVs if possible
468 469 470
  if (mbmi->mv[0].as_int == 0 && sse < ctx->zeromv_sse) {
    ctx->zeromv_sse = sse;
    ctx->best_zeromv_reference_frame = mbmi->ref_frame[0];
471 472
    if (mbmi->ref_frame[0] == LAST_FRAME)
      ctx->zeromv_lastref_sse = sse;
473 474
  }

Marco's avatar
Marco committed
475
  if (mbmi->mv[0].as_int != 0 && sse < ctx->newmv_sse) {
476 477 478 479
    ctx->newmv_sse = sse;
    ctx->best_sse_inter_mode = mode;
    ctx->best_sse_mv = mbmi->mv[0];
    ctx->best_reference_frame = mbmi->ref_frame[0];
480
  }
Tim Kopp's avatar
Tim Kopp committed
481 482 483
}

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

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

509
  fail = vpx_alloc_frame_buffer(&denoiser->mc_running_avg_y, width, height,
510 511 512
                                ssx, ssy,
#if CONFIG_VP9_HIGHBITDEPTH
                                use_highbitdepth,
513 514 515 516 517 518 519 520 521 522 523
#endif
                                border, legacy_byte_alignment);
  if (fail) {
    vp9_denoiser_free(denoiser);
    return 1;
  }

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

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

552 553 554
void vp9_denoiser_set_noise_level(VP9_DENOISER *denoiser,
                                  int noise_level) {
  denoiser->denoising_level = noise_level;
Tim Kopp's avatar
Tim Kopp committed
555
}
Tim Kopp's avatar
Tim Kopp committed
556 557 558 559 560 561 562

#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;

563 564
  for (r = 0; r < yuv->uv_height; ++r) {
    for (c = 0; c < yuv->uv_width; ++c) {
Tim Kopp's avatar
Tim Kopp committed
565 566 567
      u[c] = UINT8_MAX / 2;
      v[c] = UINT8_MAX / 2;
    }
568 569
    u += yuv->uv_stride;
    v += yuv->uv_stride;
Tim Kopp's avatar
Tim Kopp committed
570 571 572
  }
}
#endif