vp9_noise_estimate.c 9.34 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*
 *  Copyright (c) 2015 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.
 */

#include <assert.h>
#include <limits.h>
#include <math.h>

#include "./vpx_dsp_rtcd.h"
#include "vpx_dsp/vpx_dsp_common.h"
#include "vpx_scale/yv12config.h"
#include "vpx/vpx_integer.h"
#include "vp9/common/vp9_reconinter.h"
#include "vp9/encoder/vp9_context_tree.h"
#include "vp9/encoder/vp9_noise_estimate.h"
#include "vp9/encoder/vp9_encoder.h"

void vp9_noise_estimate_init(NOISE_ESTIMATE *const ne,
                             int width,
                             int height) {
  ne->enabled = 0;
28
  ne->level = kLowLow;
29
30
  ne->value = 0;
  ne->count = 0;
Marco's avatar
Marco committed
31
  ne->thresh = 90;
32
33
  ne->last_w = 0;
  ne->last_h = 0;
34
  if (width * height >= 1920 * 1080) {
Marco's avatar
Marco committed
35
    ne->thresh = 200;
36
  } else if (width * height >= 1280 * 720) {
Marco's avatar
Marco committed
37
    ne->thresh = 130;
38
  }
39
  ne->num_frames_estimate = 20;
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
}

int enable_noise_estimation(VP9_COMP *const cpi) {
  // Enable noise estimation if denoising is on (and cyclic refresh, since
  // noise estimate is currently using a struct defined in cyclic refresh).
#if CONFIG_VP9_TEMPORAL_DENOISING
  if (cpi->oxcf.noise_sensitivity > 0 &&
      cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
    return 1;
#endif
  // Only allow noise estimate under certain encoding mode.
  // Enabled for 1 pass CBR, speed >=5, and if resolution is same as original.
  // Not enabled for SVC mode and screen_content_mode.
  // Not enabled for low resolutions.
  if (cpi->oxcf.pass == 0 &&
      cpi->oxcf.rc_mode == VPX_CBR &&
      cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ &&
      cpi->oxcf.speed >= 5 &&
      cpi->resize_state == ORIG &&
59
      cpi->resize_pending == 0 &&
60
61
      !cpi->use_svc &&
      cpi->oxcf.content != VP9E_CONTENT_SCREEN &&
Marco's avatar
Marco committed
62
63
      cpi->common.width >= 640 &&
      cpi->common.height >= 480)
64
65
66
67
68
    return 1;
  else
    return 0;
}

69
#if CONFIG_VP9_TEMPORAL_DENOISING
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
static void copy_frame(YV12_BUFFER_CONFIG * const dest,
                       const YV12_BUFFER_CONFIG * const src) {
  int r;
  const uint8_t *srcbuf = src->y_buffer;
  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) {
    memcpy(destbuf, srcbuf, dest->y_width);
    destbuf += dest->y_stride;
    srcbuf += src->y_stride;
  }
}
85
#endif  // CONFIG_VP9_TEMPORAL_DENOISING
86

87
NOISE_LEVEL vp9_noise_estimate_extract_level(NOISE_ESTIMATE *const ne) {
88
  int noise_level = kLowLow;
89
90
91
92
93
94
95
96
97
98
99
100
101
  if (ne->value > (ne->thresh << 1)) {
    noise_level = kHigh;
  } else {
    if (ne->value > ne->thresh)
      noise_level = kMedium;
    else if (ne->value > (ne->thresh >> 1))
      noise_level = kLow;
    else
      noise_level = kLowLow;
  }
  return noise_level;
}

102
103
104
105
106
107
108
void vp9_update_noise_estimate(VP9_COMP *const cpi) {
  const VP9_COMMON *const cm = &cpi->common;
  CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
  NOISE_ESTIMATE *const ne = &cpi->noise_estimate;
  // Estimate of noise level every frame_period frames.
  int frame_period = 10;
  int thresh_consec_zeromv = 8;
109
  unsigned int thresh_sum_diff = 100;
Marco's avatar
Marco committed
110
111
  unsigned int thresh_sum_spatial = (200 * 200) << 8;
  unsigned int thresh_spatial_var = (32 * 32) << 8;
112
113
114
115
116
117
118
119
120
121
  int min_blocks_estimate = cm->mi_rows * cm->mi_cols >> 7;
  // Estimate is between current source and last source.
  YV12_BUFFER_CONFIG *last_source = cpi->Last_Source;
#if CONFIG_VP9_TEMPORAL_DENOISING
  if (cpi->oxcf.noise_sensitivity > 0)
    last_source = &cpi->denoiser.last_source;
#endif
  ne->enabled = enable_noise_estimation(cpi);
  if (!ne->enabled ||
      cm->current_video_frame % frame_period != 0 ||
122
123
124
      last_source == NULL ||
      ne->last_w != cm->width ||
      ne->last_h != cm->height) {
125
126
127
128
#if CONFIG_VP9_TEMPORAL_DENOISING
  if (cpi->oxcf.noise_sensitivity > 0)
    copy_frame(&cpi->denoiser.last_source, cpi->Source);
#endif
129
130
131
132
    if (last_source != NULL) {
      ne->last_w = cm->width;
      ne->last_h = cm->height;
    }
133
134
135
136
137
138
    return;
  } else {
    int num_samples = 0;
    uint64_t avg_est = 0;
    int bsize = BLOCK_16X16;
    static const unsigned char const_source[16] = {
Marco's avatar
Marco committed
139
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
140
141
142
143
144
145
146
147
148
149
150
    // Loop over sub-sample of 16x16 blocks of frame, and for blocks that have
    // been encoded as zero/small mv at least x consecutive frames, compute
    // the variance to update estimate of noise in the source.
    const uint8_t *src_y = cpi->Source->y_buffer;
    const int src_ystride = cpi->Source->y_stride;
    const uint8_t *last_src_y = last_source->y_buffer;
    const int last_src_ystride = last_source->y_stride;
    const uint8_t *src_u = cpi->Source->u_buffer;
    const uint8_t *src_v = cpi->Source->v_buffer;
    const int src_uvstride = cpi->Source->uv_stride;
    int mi_row, mi_col;
151
152
153
154
155
156
157
158
159
160
161
    int num_low_motion = 0;
    int frame_low_motion = 1;
    for (mi_row = 0; mi_row < cm->mi_rows; mi_row++) {
      for (mi_col = 0; mi_col < cm->mi_cols; mi_col++) {
        int bl_index = mi_row * cm->mi_cols + mi_col;
        if (cr->consec_zero_mv[bl_index] > thresh_consec_zeromv)
          num_low_motion++;
      }
    }
    if (num_low_motion < ((3 * cm->mi_rows * cm->mi_cols) >> 3))
      frame_low_motion = 0;
162
163
164
    for (mi_row = 0; mi_row < cm->mi_rows; mi_row++) {
      for (mi_col = 0; mi_col < cm->mi_cols; mi_col++) {
        // 16x16 blocks, 1/4 sample of frame.
165
166
167
        if (mi_row % 4 == 0 && mi_col % 4 == 0 &&
            mi_row < cm->mi_rows - 1 &&
            mi_col < cm->mi_cols - 1) {
168
169
170
171
172
173
174
175
          int bl_index = mi_row * cm->mi_cols + mi_col;
          int bl_index1 = bl_index + 1;
          int bl_index2 = bl_index + cm->mi_cols;
          int bl_index3 = bl_index2 + 1;
          // Only consider blocks that are likely steady background. i.e, have
          // been encoded as zero/low motion x (= thresh_consec_zeromv) frames
          // in a row. consec_zero_mv[] defined for 8x8 blocks, so consider all
          // 4 sub-blocks for 16x16 block. Also, avoid skin blocks.
Marco's avatar
Marco committed
176
177
178
179
180
181
          int is_skin = vp9_compute_skin_block(src_y,
                                               src_u,
                                               src_v,
                                               src_ystride,
                                               src_uvstride,
                                               bsize);
182
183
          if (frame_low_motion &&
              cr->consec_zero_mv[bl_index] > thresh_consec_zeromv &&
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
              cr->consec_zero_mv[bl_index1] > thresh_consec_zeromv &&
              cr->consec_zero_mv[bl_index2] > thresh_consec_zeromv &&
              cr->consec_zero_mv[bl_index3] > thresh_consec_zeromv &&
              !is_skin) {
            // Compute variance.
            unsigned int sse;
            unsigned int variance = cpi->fn_ptr[bsize].vf(src_y,
                                                          src_ystride,
                                                          last_src_y,
                                                          last_src_ystride,
                                                          &sse);
            // Only consider this block as valid for noise measurement if the
            // average term (sse - variance = N * avg^{2}, N = 16X16) of the
            // temporal residual is small (avoid effects from lighting change).
            if ((sse - variance) < thresh_sum_diff) {
              unsigned int sse2;
              const unsigned int spatial_variance =
                  cpi->fn_ptr[bsize].vf(src_y, src_ystride, const_source,
                                        0, &sse2);
Marco's avatar
Marco committed
203
204
205
206
207
208
              // Avoid blocks with high brightness and high spatial variance.
              if ((sse2 - spatial_variance) < thresh_sum_spatial &&
                  spatial_variance < thresh_spatial_var) {
                avg_est += variance / ((spatial_variance >> 9) + 1);
                num_samples++;
              }
209
210
211
212
213
214
215
216
217
218
219
220
221
            }
          }
        }
        src_y += 8;
        last_src_y += 8;
        src_u += 4;
        src_v += 4;
      }
      src_y += (src_ystride << 3) - (cm->mi_cols << 3);
      last_src_y += (last_src_ystride << 3) - (cm->mi_cols << 3);
      src_u += (src_uvstride << 2) - (cm->mi_cols << 2);
      src_v += (src_uvstride << 2) - (cm->mi_cols << 2);
    }
222
223
    ne->last_w = cm->width;
    ne->last_h = cm->height;
224
225
226
227
228
    // Update noise estimate if we have at a minimum number of block samples,
    // and avg_est > 0 (avg_est == 0 can happen if the application inputs
    // duplicate frames).
    if (num_samples > min_blocks_estimate && avg_est > 0) {
      // Normalize.
Marco's avatar
Marco committed
229
      avg_est = avg_est / num_samples;
230
      // Update noise estimate.
231
      ne->value = (int)((15 * ne->value + avg_est) >> 4);
232
      ne->count++;
233
      if (ne->count == ne->num_frames_estimate) {
234
        // Reset counter and check noise level condition.
235
        ne->num_frames_estimate = 30;
236
        ne->count = 0;
237
        ne->level = vp9_noise_estimate_extract_level(ne);
238
239
240
241
#if CONFIG_VP9_TEMPORAL_DENOISING
        if (cpi->oxcf.noise_sensitivity > 0)
          vp9_denoiser_set_noise_level(&cpi->denoiser, ne->level);
#endif
242
243
244
245
      }
    }
  }
#if CONFIG_VP9_TEMPORAL_DENOISING
246
  if (cpi->oxcf.noise_sensitivity > 0)
247
248
249
    copy_frame(&cpi->denoiser.last_source, cpi->Source);
#endif
}