vp9_denoiser.c 13.5 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"
Tim Kopp's avatar
Tim Kopp committed
16
#include "vp9/encoder/vp9_denoiser.h"
Tim Kopp's avatar
Tim Kopp committed
17

18
19
20
21
22
23
24
25
26
27
28
/* 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
29
30
31
32
#ifdef OUTPUT_YUV_DENOISED
static void make_grayscale(YV12_BUFFER_CONFIG *yuv);
#endif

Tim Kopp's avatar
Tim Kopp committed
33
34
35
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};

36
37
38
39
40
41
42
static VP9_DENOISER_DECISION update_running_avg(const uint8_t *mc_avg,
                                                int mc_avg_stride,
                                                uint8_t *avg, int avg_stride,
                                                const uint8_t *sig,
                                                int sig_stride,
                                                int increase_denoising,
                                                BLOCK_SIZE bs) {
Tim Kopp's avatar
Tim Kopp committed
43
  int r, c;
44
45
46
  int diff, adj, absdiff;
  int shift_inc1 = 0, shift_inc2 = 1;
  int adj_val[] = {3, 4, 6};
Tim Kopp's avatar
Tim Kopp committed
47
48
  int total_adj = 0;

49
50
51
52
53
  if (increase_denoising) {
    shift_inc1 = 1;
    shift_inc2 = 2;
  }

Tim Kopp's avatar
Tim Kopp committed
54
55
56
  for (r = 0; r < heights[bs]; ++r) {
    for (c = 0; c < widths[bs]; ++c) {
      diff = mc_avg[c] - sig[c];
57
      absdiff = abs(diff);
Tim Kopp's avatar
Tim Kopp committed
58

59
60
61
      if (absdiff <= 3 + shift_inc1) {
        avg[c] = mc_avg[c];
        total_adj += diff;
Tim Kopp's avatar
Tim Kopp committed
62
      } else {
63
64
65
        switch (absdiff) {
          case 4: case 5: case 6: case 7:
            adj = adj_val[0];
Tim Kopp's avatar
Tim Kopp committed
66
            break;
67
68
69
          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
70
            break;
71
72
73
74
75
76
77
78
79
80
          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
81
82
83
84
85
86
87
88
89
      }
    }
    sig += sig_stride;
    avg += avg_stride;
    mc_avg += mc_avg_stride;
  }
  return total_adj;
}

Tim Kopp's avatar
Tim Kopp committed
90
91
static uint8_t *block_start(uint8_t *framebuf, int stride,
                            int mi_row, int mi_col) {
92
  return framebuf + (stride * mi_row * 8) + (mi_col * 8);
93
94
}

Tim Kopp's avatar
Tim Kopp committed
95
96
97
static void copy_block(uint8_t *dest, int dest_stride,
                       const uint8_t *src, int src_stride, BLOCK_SIZE bs) {
  int r;
98
  for (r = 0; r < heights[bs]; ++r) {
Tim Kopp's avatar
Tim Kopp committed
99
    vpx_memcpy(dest, src, widths[bs]);
100
101
102
103
104
    dest += dest_stride;
    src += src_stride;
  }
}

105
106
107
108
109
110
static VP9_DENOISER_DECISION perform_motion_compensation(VP9_DENOISER *denoiser,
                                                         MACROBLOCK *mb,
                                                         BLOCK_SIZE bs,
                                                         int increase_denoising,
                                                         int mi_row,
                                                         int mi_col) {
111
112
113
114
115
116
117
118
119
120
121
122
123
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
  // constants
  // TODO(tkopp): empirically determine good constants, or functions of block
  // size.
  int NOISE_MOTION_THRESHOLD = 25 * 25;
  int SSE_DIFF_THRESHOLD = heights[bs] * widths[bs] * 20;
  unsigned int SSE_THRESH = heights[bs] * widths[bs] * 40;
  unsigned int SSE_THRESH_HI = heights[bs] * widths[bs] * 60;

  int mv_col, mv_row;
  int sse_diff = denoiser->zero_mv_sse - denoiser->best_sse;
  int sse_diff_thresh;
  int sse_thresh;
  MV_REFERENCE_FRAME frame;
  MACROBLOCKD *filter_mbd = &mb->e_mbd;
  MB_MODE_INFO *mbmi = &filter_mbd->mi[0]->mbmi;

  // We will restore these after motion compensation.
  MB_MODE_INFO saved_mbmi = *mbmi;
  struct buf_2d saved_dst = filter_mbd->plane[0].dst;
  struct buf_2d saved_pre[2];
  saved_pre[0] = filter_mbd->plane[0].pre[0];
  saved_pre[1] = filter_mbd->plane[0].pre[1];

  // Decide the threshold for sum squared error.
  mv_col = denoiser->best_sse_mv.as_mv.col;
  mv_row = denoiser->best_sse_mv.as_mv.row;
  if (mv_row * mv_row + mv_col * mv_col > NOISE_MOTION_THRESHOLD) {
    sse_diff_thresh = 0;
  } else {
    sse_diff_thresh = SSE_DIFF_THRESHOLD;
  }

  frame = denoiser->best_reference_frame;

  // If the best reference frame uses inter-prediction and there is enough of a
  // difference in sum-squared-error, use it.
  if (frame != INTRA_FRAME && sse_diff > sse_diff_thresh) {
    mbmi->ref_frame[0] = denoiser->best_reference_frame;
    mbmi->mode = denoiser->best_sse_inter_mode;
    mbmi->mv[0] = denoiser->best_sse_mv;
  } else {
    // Otherwise, use the zero reference frame.
    frame = denoiser->best_zeromv_reference_frame;

    mbmi->ref_frame[0] = denoiser->best_zeromv_reference_frame;
    mbmi->mode = ZEROMV;
    mbmi->mv[0].as_int = 0;

    denoiser->best_sse_inter_mode = ZEROMV;
    denoiser->best_sse_mv.as_int = 0;
    denoiser->best_sse = denoiser->zero_mv_sse;
  }

  // Set the pointers in the MACROBLOCKD to point to the buffers in the denoiser
  // struct.
  filter_mbd->plane[0].pre[0].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[0].stride = denoiser->running_avg_y[frame].y_stride;

  filter_mbd->plane[1].pre[0].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[0].stride = denoiser->running_avg_y[frame].uv_stride;

  filter_mbd->plane[2].pre[0].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[0].stride = denoiser->running_avg_y[frame].uv_stride;

  filter_mbd->plane[0].pre[1].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[1].stride = denoiser->running_avg_y[frame].y_stride;

  filter_mbd->plane[1].pre[1].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[1].stride = denoiser->running_avg_y[frame].uv_stride;

  filter_mbd->plane[2].pre[1].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[1].stride = denoiser->running_avg_y[frame].uv_stride;

  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);
  filter_mbd->plane[1].dst.stride = denoiser->mc_running_avg_y.y_stride;

  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);
  filter_mbd->plane[2].dst.stride = denoiser->mc_running_avg_y.y_stride;

  vp9_build_inter_predictors_sby(filter_mbd, mv_row, mv_col, bs);

  // Restore everything to its original state
  filter_mbd->plane[0].pre[0] = saved_pre[0];
  filter_mbd->plane[0].pre[1] = saved_pre[1];
  filter_mbd->plane[0].dst = saved_dst;
  *mbmi = saved_mbmi;

  mv_row = denoiser->best_sse_mv.as_mv.row;
  mv_col = denoiser->best_sse_mv.as_mv.col;
  sse_thresh = denoiser->increase_denoising ? SSE_THRESH_HI : SSE_THRESH;

  // TODO(tkopp) why 8?
  if (denoiser->best_sse > sse_thresh ||
    mv_row * mv_row + mv_col * mv_col > 8 * NOISE_MOTION_THRESHOLD) {
    return COPY_BLOCK;
  }
  return FILTER_BLOCK;
}

240
void vp9_denoiser_denoise(VP9_DENOISER *denoiser, MACROBLOCK *mb,
Tim Kopp's avatar
Tim Kopp committed
241
                          int mi_row, int mi_col, BLOCK_SIZE bs) {
242
  VP9_DENOISER_DECISION decision = FILTER_BLOCK;
243
244

  YV12_BUFFER_CONFIG avg = denoiser->running_avg_y[INTRA_FRAME];
Tim Kopp's avatar
Tim Kopp committed
245
246
247
248
  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);
249
250
  struct buf_2d src = mb->plane[0].src;

251
252
253
  decision = perform_motion_compensation(denoiser, mb, bs,
                                         denoiser->increase_denoising,
                                         mi_row, mi_col);
Tim Kopp's avatar
Tim Kopp committed
254
  update_running_avg(mc_avg_start, mc_avg.y_stride, avg_start, avg.y_stride,
255
                     mb->plane[0].src.buf, mb->plane[0].src.stride, 0, bs);
256
257

  if (decision == FILTER_BLOCK) {
Tim Kopp's avatar
Tim Kopp committed
258
    // TODO(tkopp)
259
260
  }
  if (decision == COPY_BLOCK) {
Tim Kopp's avatar
Tim Kopp committed
261
    copy_block(avg_start, avg.y_stride, src.buf, src.stride, bs);
262
  }
Tim Kopp's avatar
Tim Kopp committed
263
264
}

Tim Kopp's avatar
Tim Kopp committed
265
static void copy_frame(YV12_BUFFER_CONFIG dest, const YV12_BUFFER_CONFIG src) {
Tim Kopp's avatar
Tim Kopp committed
266
  int r;
Tim Kopp's avatar
Tim Kopp committed
267
  const uint8_t *srcbuf = src.y_buffer;
268
269
270
271
272
  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
273
    vpx_memcpy(destbuf, srcbuf, dest.y_width);
274
275
276
    destbuf += dest.y_stride;
    srcbuf += src.y_stride;
  }
277
278
}

Tim Kopp's avatar
Tim Kopp committed
279
void vp9_denoiser_update_frame_info(VP9_DENOISER *denoiser,
280
                                    YV12_BUFFER_CONFIG src,
Tim Kopp's avatar
Tim Kopp committed
281
282
283
284
                                    FRAME_TYPE frame_type,
                                    int refresh_alt_ref_frame,
                                    int refresh_golden_frame,
                                    int refresh_last_frame) {
285
  if (frame_type == KEY_FRAME) {
Tim Kopp's avatar
Tim Kopp committed
286
    int i;
287
288
289
290
291
    copy_frame(denoiser->running_avg_y[LAST_FRAME], src);
    for (i = 2; i < MAX_REF_FRAMES - 1; i++) {
      copy_frame(denoiser->running_avg_y[i],
                 denoiser->running_avg_y[LAST_FRAME]);
    }
Tim Kopp's avatar
Tim Kopp committed
292
  } else {  /* For non key frames */
293
294
295
296
297
298
299
300
301
302
303
304
305
    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
306
307
}

308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
void vp9_denoiser_reset_frame_stats(VP9_DENOISER *denoiser) {
  denoiser->zero_mv_sse = UINT_MAX;
  denoiser->best_sse = UINT_MAX;
}

void vp9_denoiser_update_frame_stats(VP9_DENOISER *denoiser, MB_MODE_INFO *mbmi,
                                     unsigned int sse, PREDICTION_MODE mode) {
  // TODO(tkopp): Use both MVs if possible
  if (mbmi->mv[0].as_int == 0 && sse < denoiser->zero_mv_sse) {
    denoiser->zero_mv_sse = sse;
    denoiser->best_zeromv_reference_frame = mbmi->ref_frame[0];
  }

  if (mbmi->mv[0].as_int != 0 && sse < denoiser->best_sse) {
    denoiser->best_sse = sse;
    denoiser->best_sse_inter_mode = mode;
    denoiser->best_sse_mv = mbmi->mv[0];
    denoiser->best_reference_frame = mbmi->ref_frame[0];
  }
Tim Kopp's avatar
Tim Kopp committed
327
328
329
}

int vp9_denoiser_alloc(VP9_DENOISER *denoiser, int width, int height,
330
331
                       int ssx, int ssy, int border) {
  int i, fail;
Tim Kopp's avatar
Tim Kopp committed
332
  assert(denoiser != NULL);
333

334
335
  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
336
                                  ssx, ssy, border);
337
338
339
340
    if (fail) {
      vp9_denoiser_free(denoiser);
      return 1;
    }
Tim Kopp's avatar
Tim Kopp committed
341
342
343
#ifdef OUTPUT_YUV_DENOISED
    make_grayscale(&denoiser->running_avg_y[i]);
#endif
344
345
  }

346
  fail = vp9_alloc_frame_buffer(&denoiser->mc_running_avg_y, width, height,
Tim Kopp's avatar
Tim Kopp committed
347
                                ssx, ssy, border);
348
  if (fail) {
349
350
351
    vp9_denoiser_free(denoiser);
    return 1;
  }
Tim Kopp's avatar
Tim Kopp committed
352
353
354
#ifdef OUTPUT_YUV_DENOISED
  make_grayscale(&denoiser->running_avg_y[i]);
#endif
355
356
  denoiser->increase_denoising = 0;

Tim Kopp's avatar
Tim Kopp committed
357
358
359
360
  return 0;
}

void vp9_denoiser_free(VP9_DENOISER *denoiser) {
361
  int i;
Tim Kopp's avatar
Tim Kopp committed
362
363
364
  if (denoiser == NULL) {
    return;
  }
365
366
367
368
  for (i = 0; i < MAX_REF_FRAMES; ++i) {
    if (&denoiser->running_avg_y[i] != NULL) {
      vp9_free_frame_buffer(&denoiser->running_avg_y[i]);
    }
369
  }
370
371
  if (&denoiser->mc_running_avg_y != NULL) {
    vp9_free_frame_buffer(&denoiser->mc_running_avg_y);
372
  }
Tim Kopp's avatar
Tim Kopp committed
373
}
Tim Kopp's avatar
Tim Kopp committed
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392

#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