vp9_denoiser.c 14.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>
Tim Kopp's avatar
Tim Kopp committed
13
#include "vpx_scale/yv12config.h"
Tim Kopp's avatar
Tim Kopp committed
14
#include "vpx/vpx_integer.h"
15
#include "vp9/common/vp9_reconinter.h"
16
#include "vp9/encoder/vp9_context_tree.h"
Tim Kopp's avatar
Tim Kopp committed
17
#include "vp9/encoder/vp9_denoiser.h"
Tim Kopp's avatar
Tim Kopp committed
18

19 20 21 22 23 24 25 26 27 28 29
/* The VP9 denoiser is a work-in-progress. It currently is only designed to work
 * with speed 6, though it (inexplicably) seems to also work with speed 5 (one
 * would need to modify the source code in vp9_pickmode.c and vp9_encoder.c to
 * make the calls to the vp9_denoiser_* functions when in speed 5).
 *
 * The implementation is very similar to that of the VP8 denoiser. While
 * 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

Tim Kopp's avatar
Tim Kopp committed
34 35 36
static const int widths[]  = {4, 4, 8, 8,  8, 16, 16, 16, 32, 32, 32, 64, 64};
static const int heights[] = {4, 8, 4, 8, 16,  8, 16, 32, 16, 32, 64, 32, 64};

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
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;
  return 25 * 25;
}

static unsigned int sse_thresh(BLOCK_SIZE bs, int increase_denoising) {
  return widths[bs] * heights[bs] * (increase_denoising ? 60 : 40);
}

static int sse_diff_thresh(BLOCK_SIZE bs, int increase_denoising,
                           int mv_row, int mv_col) {
  if (mv_row * mv_row + mv_col * mv_col >
      noise_motion_thresh(bs, increase_denoising)) {
    return 0;
  } else {
    return widths[bs] * heights[bs] * 20;
  }
}

static int total_adj_strong_thresh(BLOCK_SIZE bs, int increase_denoising) {
  return widths[bs] * heights[bs] * (increase_denoising ? 3 : 2);
}

static int total_adj_weak_thresh(BLOCK_SIZE bs, int increase_denoising) {
  return widths[bs] * heights[bs] * (increase_denoising ? 3 : 2);
}

static VP9_DENOISER_DECISION denoiser_filter(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) {
Tim Kopp's avatar
Tim Kopp committed
82
  int r, c;
83 84 85 86
  const uint8_t *sig_start = sig;
  const uint8_t *mc_avg_start = mc_avg;
  uint8_t *avg_start = avg;
  int diff, adj, absdiff, delta;
87
  int adj_val[] = {3, 4, 6};
Tim Kopp's avatar
Tim Kopp committed
88 89
  int total_adj = 0;

90
  // First attempt to apply a strong temporal denoising filter.
Tim Kopp's avatar
Tim Kopp committed
91 92 93
  for (r = 0; r < heights[bs]; ++r) {
    for (c = 0; c < widths[bs]; ++c) {
      diff = mc_avg[c] - sig[c];
94
      absdiff = abs(diff);
Tim Kopp's avatar
Tim Kopp committed
95

96
      if (absdiff <= absdiff_thresh(bs, increase_denoising)) {
97 98
        avg[c] = mc_avg[c];
        total_adj += diff;
Tim Kopp's avatar
Tim Kopp committed
99
      } else {
100 101 102
        switch (absdiff) {
          case 4: case 5: case 6: case 7:
            adj = adj_val[0];
Tim Kopp's avatar
Tim Kopp committed
103
            break;
104 105 106
          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
107
            break;
108 109 110 111 112 113 114 115 116 117
          default:
            adj = adj_val[2];
        }
        if (diff > 0) {
          avg[c] = MIN(UINT8_MAX, sig[c] + adj);
          total_adj += adj;
        } else {
          avg[c] = MAX(0, sig[c] - adj);
          total_adj -= adj;
        }
Tim Kopp's avatar
Tim Kopp committed
118 119 120 121 122 123
      }
    }
    sig += sig_stride;
    avg += avg_stride;
    mc_avg += mc_avg_stride;
  }
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164

  // 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))
           >> 8) + 1;
  if (delta > delta_thresh(bs, increase_denoising)) {
    return COPY_BLOCK;
  }

  mc_avg =  mc_avg_start;
  avg = avg_start;
  sig = sig_start;
  for (r = 0; r < heights[bs]; ++r) {
    for (c = 0; c < widths[bs]; ++c) {
      diff = mc_avg[c] - sig[c];
      adj = abs(diff);
      if (adj > delta) {
        adj = delta;
      }
      if (diff > 0) {
        avg[c] = MAX(0, avg[c] - adj);
        total_adj += adj;
      } else {
        avg[c] = MIN(UINT8_MAX, avg[c] + adj);
        total_adj -= adj;
      }
    }
    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
165 166
}

Tim Kopp's avatar
Tim Kopp committed
167 168
static uint8_t *block_start(uint8_t *framebuf, int stride,
                            int mi_row, int mi_col) {
169
  return framebuf + (stride * mi_row * 8) + (mi_col * 8);
170 171
}

Tim Kopp's avatar
Tim Kopp committed
172 173 174
static void copy_block(uint8_t *dest, int dest_stride,
                       const uint8_t *src, int src_stride, BLOCK_SIZE bs) {
  int r;
175
  for (r = 0; r < heights[bs]; ++r) {
Tim Kopp's avatar
Tim Kopp committed
176
    vpx_memcpy(dest, src, widths[bs]);
177 178 179 180 181
    dest += dest_stride;
    src += src_stride;
  }
}

182 183 184 185 186
static VP9_DENOISER_DECISION perform_motion_compensation(VP9_DENOISER *denoiser,
                                                         MACROBLOCK *mb,
                                                         BLOCK_SIZE bs,
                                                         int increase_denoising,
                                                         int mi_row,
187 188 189
                                                         int mi_col,
                                                         PICK_MODE_CONTEXT *ctx
                                                         ) {
190
  int mv_col, mv_row;
191
  int sse_diff = ctx->zeromv_sse - ctx->newmv_sse;
192 193 194 195
  MV_REFERENCE_FRAME frame;
  MACROBLOCKD *filter_mbd = &mb->e_mbd;
  MB_MODE_INFO *mbmi = &filter_mbd->mi[0]->mbmi;

Tim Kopp's avatar
Tim Kopp committed
196 197 198 199 200
  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

201
  // We will restore these after motion compensation.
Tim Kopp's avatar
Tim Kopp committed
202 203 204 205 206 207 208
  saved_mbmi = *mbmi;
  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;
  }
209

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

213
  frame = ctx->best_reference_frame;
214 215 216

  // If the best reference frame uses inter-prediction and there is enough of a
  // difference in sum-squared-error, use it.
217 218
  if (frame != INTRA_FRAME &&
      sse_diff > sse_diff_thresh(bs, increase_denoising, mv_row, mv_col)) {
219 220 221
    mbmi->ref_frame[0] = ctx->best_reference_frame;
    mbmi->mode = ctx->best_sse_inter_mode;
    mbmi->mv[0] = ctx->best_sse_mv;
222 223
  } else {
    // Otherwise, use the zero reference frame.
224
    frame = ctx->best_zeromv_reference_frame;
225

226
    mbmi->ref_frame[0] = ctx->best_zeromv_reference_frame;
227 228 229
    mbmi->mode = ZEROMV;
    mbmi->mv[0].as_int = 0;

230 231 232
    ctx->best_sse_inter_mode = ZEROMV;
    ctx->best_sse_mv.as_int = 0;
    ctx->newmv_sse = ctx->zeromv_sse;
233 234 235 236
  }

  // Set the pointers in the MACROBLOCKD to point to the buffers in the denoiser
  // struct.
Tim Kopp's avatar
Tim Kopp committed
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
  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;
  }
257 258 259 260 261 262 263 264 265
  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
266
  filter_mbd->plane[1].dst.stride = denoiser->mc_running_avg_y.uv_stride;
267 268 269 270
  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
271
  filter_mbd->plane[2].dst.stride = denoiser->mc_running_avg_y.uv_stride;
272 273 274 275 276

  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
277 278 279 280 281 282
  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];
  }
283

284 285
  mv_row = ctx->best_sse_mv.as_mv.row;
  mv_col = ctx->best_sse_mv.as_mv.col;
286

287
  if (ctx->newmv_sse > sse_thresh(bs, increase_denoising)) {
288 289 290 291
    return COPY_BLOCK;
  }
  if (mv_row * mv_row + mv_col * mv_col >
      8 * noise_motion_thresh(bs, increase_denoising)) {
292 293 294 295 296
    return COPY_BLOCK;
  }
  return FILTER_BLOCK;
}

297
void vp9_denoiser_denoise(VP9_DENOISER *denoiser, MACROBLOCK *mb,
298 299
                          int mi_row, int mi_col, BLOCK_SIZE bs,
                          PICK_MODE_CONTEXT *ctx) {
300
  VP9_DENOISER_DECISION decision = FILTER_BLOCK;
301
  YV12_BUFFER_CONFIG avg = denoiser->running_avg_y[INTRA_FRAME];
Tim Kopp's avatar
Tim Kopp committed
302 303 304 305
  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);
306 307
  struct buf_2d src = mb->plane[0].src;

308 309
  decision = perform_motion_compensation(denoiser, mb, bs,
                                         denoiser->increase_denoising,
310
                                         mi_row, mi_col, ctx);
311 312

  if (decision == FILTER_BLOCK) {
313 314 315 316
    decision = denoiser_filter(src.buf, src.stride,
                               mc_avg_start, mc_avg.y_stride,
                               avg_start, avg.y_stride,
                               0, bs);
317
  }
318 319 320 321

  if (decision == FILTER_BLOCK) {
    copy_block(src.buf, src.stride, avg_start, avg.y_stride, bs);
  } else {  // COPY_BLOCK
Tim Kopp's avatar
Tim Kopp committed
322
    copy_block(avg_start, avg.y_stride, src.buf, src.stride, bs);
323
  }
Tim Kopp's avatar
Tim Kopp committed
324 325
}

Tim Kopp's avatar
Tim Kopp committed
326
static void copy_frame(YV12_BUFFER_CONFIG dest, const YV12_BUFFER_CONFIG src) {
Tim Kopp's avatar
Tim Kopp committed
327
  int r;
Tim Kopp's avatar
Tim Kopp committed
328
  const uint8_t *srcbuf = src.y_buffer;
329 330 331 332 333
  uint8_t *destbuf = dest.y_buffer;
  assert(dest.y_width == src.y_width);
  assert(dest.y_height == src.y_height);

  for (r = 0; r < dest.y_height; ++r) {
Tim Kopp's avatar
Tim Kopp committed
334
    vpx_memcpy(destbuf, srcbuf, dest.y_width);
335 336 337
    destbuf += dest.y_stride;
    srcbuf += src.y_stride;
  }
338 339
}

Tim Kopp's avatar
Tim Kopp committed
340
void vp9_denoiser_update_frame_info(VP9_DENOISER *denoiser,
341
                                    YV12_BUFFER_CONFIG src,
Tim Kopp's avatar
Tim Kopp committed
342 343 344 345
                                    FRAME_TYPE frame_type,
                                    int refresh_alt_ref_frame,
                                    int refresh_golden_frame,
                                    int refresh_last_frame) {
346
  if (frame_type == KEY_FRAME) {
Tim Kopp's avatar
Tim Kopp committed
347
    int i;
348 349 350
    // Start at 1 so as not to overwrite the INTRA_FRAME
    for (i = 1; i < MAX_REF_FRAMES; ++i) {
      copy_frame(denoiser->running_avg_y[i], src);
351
    }
Tim Kopp's avatar
Tim Kopp committed
352
  } else {  /* For non key frames */
353 354 355 356 357 358 359 360 361 362 363 364 365
    if (refresh_alt_ref_frame) {
      copy_frame(denoiser->running_avg_y[ALTREF_FRAME],
                 denoiser->running_avg_y[INTRA_FRAME]);
    }
    if (refresh_golden_frame) {
      copy_frame(denoiser->running_avg_y[GOLDEN_FRAME],
                 denoiser->running_avg_y[INTRA_FRAME]);
    }
    if (refresh_last_frame) {
      copy_frame(denoiser->running_avg_y[LAST_FRAME],
                 denoiser->running_avg_y[INTRA_FRAME]);
    }
  }
Tim Kopp's avatar
Tim Kopp committed
366 367
}

368 369 370
void vp9_denoiser_reset_frame_stats(PICK_MODE_CONTEXT *ctx) {
  ctx->zeromv_sse = UINT_MAX;
  ctx->newmv_sse = UINT_MAX;
371 372 373
}

void vp9_denoiser_update_frame_stats(VP9_DENOISER *denoiser, MB_MODE_INFO *mbmi,
374 375
                                     unsigned int sse, PREDICTION_MODE mode,
                                     PICK_MODE_CONTEXT *ctx) {
376
  // TODO(tkopp): Use both MVs if possible
377 378 379
  if (mbmi->mv[0].as_int == 0 && sse < ctx->zeromv_sse) {
    ctx->zeromv_sse = sse;
    ctx->best_zeromv_reference_frame = mbmi->ref_frame[0];
380 381
  }

382 383 384 385 386
  if (mode == NEWMV) {
    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];
387
  }
Tim Kopp's avatar
Tim Kopp committed
388 389 390
}

int vp9_denoiser_alloc(VP9_DENOISER *denoiser, int width, int height,
391 392
                       int ssx, int ssy, int border) {
  int i, fail;
Tim Kopp's avatar
Tim Kopp committed
393
  assert(denoiser != NULL);
394

395 396
  for (i = 0; i < MAX_REF_FRAMES; ++i) {
    fail = vp9_alloc_frame_buffer(&denoiser->running_avg_y[i], width, height,
Tim Kopp's avatar
Tim Kopp committed
397
                                  ssx, ssy, border);
398 399 400 401
    if (fail) {
      vp9_denoiser_free(denoiser);
      return 1;
    }
Tim Kopp's avatar
Tim Kopp committed
402 403 404
#ifdef OUTPUT_YUV_DENOISED
    make_grayscale(&denoiser->running_avg_y[i]);
#endif
405 406
  }

407
  fail = vp9_alloc_frame_buffer(&denoiser->mc_running_avg_y, width, height,
Tim Kopp's avatar
Tim Kopp committed
408
                                ssx, ssy, border);
409
  if (fail) {
410 411 412
    vp9_denoiser_free(denoiser);
    return 1;
  }
Tim Kopp's avatar
Tim Kopp committed
413 414 415
#ifdef OUTPUT_YUV_DENOISED
  make_grayscale(&denoiser->running_avg_y[i]);
#endif
416 417
  denoiser->increase_denoising = 0;

Tim Kopp's avatar
Tim Kopp committed
418 419 420 421
  return 0;
}

void vp9_denoiser_free(VP9_DENOISER *denoiser) {
422
  int i;
Tim Kopp's avatar
Tim Kopp committed
423 424 425
  if (denoiser == NULL) {
    return;
  }
426 427 428 429
  for (i = 0; i < MAX_REF_FRAMES; ++i) {
    if (&denoiser->running_avg_y[i] != NULL) {
      vp9_free_frame_buffer(&denoiser->running_avg_y[i]);
    }
430
  }
431 432
  if (&denoiser->mc_running_avg_y != NULL) {
    vp9_free_frame_buffer(&denoiser->mc_running_avg_y);
433
  }
Tim Kopp's avatar
Tim Kopp committed
434
}
Tim Kopp's avatar
Tim Kopp committed
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453

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

  // The '/2's are there because we have a 440 buffer, but we want to output
  // 420.
  for (r = 0; r < yuv->uv_height / 2; ++r) {
    for (c = 0; c < yuv->uv_width / 2; ++c) {
      u[c] = UINT8_MAX / 2;
      v[c] = UINT8_MAX / 2;
    }
    u += yuv->uv_stride + yuv->uv_width / 2;
    v += yuv->uv_stride + yuv->uv_width / 2;
  }
}
#endif