Line data Source code
1 : /*
2 : * Copyright (c) 2015 The WebM project authors. All Rights Reserved.
3 : *
4 : * Use of this source code is governed by a BSD-style license
5 : * that can be found in the LICENSE file in the root of the source
6 : * tree. An additional intellectual property rights grant can be found
7 : * in the file PATENTS. All contributing project authors may
8 : * be found in the AUTHORS file in the root of the source tree.
9 : */
10 :
11 : #include <assert.h>
12 : #include <limits.h>
13 : #include <math.h>
14 :
15 : #include "./vpx_dsp_rtcd.h"
16 : #include "vpx_dsp/vpx_dsp_common.h"
17 : #include "vpx_scale/yv12config.h"
18 : #include "vpx/vpx_integer.h"
19 : #include "vp9/common/vp9_reconinter.h"
20 : #include "vp9/encoder/vp9_context_tree.h"
21 : #include "vp9/encoder/vp9_noise_estimate.h"
22 : #include "vp9/encoder/vp9_encoder.h"
23 :
24 0 : void vp9_noise_estimate_init(NOISE_ESTIMATE *const ne, int width, int height) {
25 0 : ne->enabled = 0;
26 0 : ne->level = kLowLow;
27 0 : ne->value = 0;
28 0 : ne->count = 0;
29 0 : ne->thresh = 100;
30 0 : ne->last_w = 0;
31 0 : ne->last_h = 0;
32 0 : if (width * height >= 1920 * 1080) {
33 0 : ne->thresh = 200;
34 0 : } else if (width * height >= 1280 * 720) {
35 0 : ne->thresh = 140;
36 : }
37 0 : ne->num_frames_estimate = 20;
38 0 : }
39 :
40 0 : static int enable_noise_estimation(VP9_COMP *const cpi) {
41 : // Enable noise estimation if denoising is on, but not for low resolutions.
42 : #if CONFIG_VP9_TEMPORAL_DENOISING
43 : if (cpi->oxcf.noise_sensitivity > 0 && cpi->common.width >= 640 &&
44 : cpi->common.height >= 360)
45 : return 1;
46 : #endif
47 : // Only allow noise estimate under certain encoding mode.
48 : // Enabled for 1 pass CBR, speed >=5, and if resolution is same as original.
49 : // Not enabled for SVC mode and screen_content_mode.
50 : // Not enabled for low resolutions.
51 0 : if (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == VPX_CBR &&
52 0 : cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cpi->oxcf.speed >= 5 &&
53 0 : cpi->resize_state == ORIG && cpi->resize_pending == 0 && !cpi->use_svc &&
54 0 : cpi->oxcf.content != VP9E_CONTENT_SCREEN && cpi->common.width >= 640 &&
55 0 : cpi->common.height >= 360)
56 0 : return 1;
57 : else
58 0 : return 0;
59 : }
60 :
61 : #if CONFIG_VP9_TEMPORAL_DENOISING
62 : static void copy_frame(YV12_BUFFER_CONFIG *const dest,
63 : const YV12_BUFFER_CONFIG *const src) {
64 : int r;
65 : const uint8_t *srcbuf = src->y_buffer;
66 : uint8_t *destbuf = dest->y_buffer;
67 :
68 : assert(dest->y_width == src->y_width);
69 : assert(dest->y_height == src->y_height);
70 :
71 : for (r = 0; r < dest->y_height; ++r) {
72 : memcpy(destbuf, srcbuf, dest->y_width);
73 : destbuf += dest->y_stride;
74 : srcbuf += src->y_stride;
75 : }
76 : }
77 : #endif // CONFIG_VP9_TEMPORAL_DENOISING
78 :
79 0 : NOISE_LEVEL vp9_noise_estimate_extract_level(NOISE_ESTIMATE *const ne) {
80 0 : int noise_level = kLowLow;
81 0 : if (ne->value > (ne->thresh << 1)) {
82 0 : noise_level = kHigh;
83 : } else {
84 0 : if (ne->value > ne->thresh)
85 0 : noise_level = kMedium;
86 0 : else if (ne->value > ((9 * ne->thresh) >> 4))
87 0 : noise_level = kLow;
88 : else
89 0 : noise_level = kLowLow;
90 : }
91 0 : return noise_level;
92 : }
93 :
94 0 : void vp9_update_noise_estimate(VP9_COMP *const cpi) {
95 0 : const VP9_COMMON *const cm = &cpi->common;
96 0 : NOISE_ESTIMATE *const ne = &cpi->noise_estimate;
97 : // Estimate of noise level every frame_period frames.
98 0 : int frame_period = 8;
99 0 : int thresh_consec_zeromv = 6;
100 0 : unsigned int thresh_sum_diff = 100;
101 0 : unsigned int thresh_sum_spatial = (200 * 200) << 8;
102 0 : unsigned int thresh_spatial_var = (32 * 32) << 8;
103 0 : int min_blocks_estimate = cm->mi_rows * cm->mi_cols >> 7;
104 : // Estimate is between current source and last source.
105 0 : YV12_BUFFER_CONFIG *last_source = cpi->Last_Source;
106 : #if CONFIG_VP9_TEMPORAL_DENOISING
107 : if (cpi->oxcf.noise_sensitivity > 0) last_source = &cpi->denoiser.last_source;
108 : #endif
109 0 : ne->enabled = enable_noise_estimation(cpi);
110 0 : if (!ne->enabled || cm->current_video_frame % frame_period != 0 ||
111 0 : last_source == NULL || ne->last_w != cm->width ||
112 0 : ne->last_h != cm->height) {
113 : #if CONFIG_VP9_TEMPORAL_DENOISING
114 : if (cpi->oxcf.noise_sensitivity > 0)
115 : copy_frame(&cpi->denoiser.last_source, cpi->Source);
116 : #endif
117 0 : if (last_source != NULL) {
118 0 : ne->last_w = cm->width;
119 0 : ne->last_h = cm->height;
120 : }
121 0 : return;
122 0 : } else if (cpi->rc.avg_frame_low_motion < 50) {
123 : // Force noise estimation to 0 and denoiser off if content has high motion.
124 0 : ne->level = kLowLow;
125 : #if CONFIG_VP9_TEMPORAL_DENOISING
126 : if (cpi->oxcf.noise_sensitivity > 0)
127 : vp9_denoiser_set_noise_level(&cpi->denoiser, ne->level);
128 : #endif
129 0 : return;
130 : } else {
131 0 : int num_samples = 0;
132 0 : uint64_t avg_est = 0;
133 0 : int bsize = BLOCK_16X16;
134 : static const unsigned char const_source[16] = { 0, 0, 0, 0, 0, 0, 0, 0,
135 : 0, 0, 0, 0, 0, 0, 0, 0 };
136 : // Loop over sub-sample of 16x16 blocks of frame, and for blocks that have
137 : // been encoded as zero/small mv at least x consecutive frames, compute
138 : // the variance to update estimate of noise in the source.
139 0 : const uint8_t *src_y = cpi->Source->y_buffer;
140 0 : const int src_ystride = cpi->Source->y_stride;
141 0 : const uint8_t *last_src_y = last_source->y_buffer;
142 0 : const int last_src_ystride = last_source->y_stride;
143 0 : const uint8_t *src_u = cpi->Source->u_buffer;
144 0 : const uint8_t *src_v = cpi->Source->v_buffer;
145 0 : const int src_uvstride = cpi->Source->uv_stride;
146 : int mi_row, mi_col;
147 0 : int num_low_motion = 0;
148 0 : int frame_low_motion = 1;
149 0 : for (mi_row = 0; mi_row < cm->mi_rows; mi_row++) {
150 0 : for (mi_col = 0; mi_col < cm->mi_cols; mi_col++) {
151 0 : int bl_index = mi_row * cm->mi_cols + mi_col;
152 0 : if (cpi->consec_zero_mv[bl_index] > thresh_consec_zeromv)
153 0 : num_low_motion++;
154 : }
155 : }
156 0 : if (num_low_motion < ((3 * cm->mi_rows * cm->mi_cols) >> 3))
157 0 : frame_low_motion = 0;
158 0 : for (mi_row = 0; mi_row < cm->mi_rows; mi_row++) {
159 0 : for (mi_col = 0; mi_col < cm->mi_cols; mi_col++) {
160 : // 16x16 blocks, 1/4 sample of frame.
161 0 : if (mi_row % 4 == 0 && mi_col % 4 == 0 && mi_row < cm->mi_rows - 1 &&
162 0 : mi_col < cm->mi_cols - 1) {
163 0 : int bl_index = mi_row * cm->mi_cols + mi_col;
164 0 : int bl_index1 = bl_index + 1;
165 0 : int bl_index2 = bl_index + cm->mi_cols;
166 0 : int bl_index3 = bl_index2 + 1;
167 : // Only consider blocks that are likely steady background. i.e, have
168 : // been encoded as zero/low motion x (= thresh_consec_zeromv) frames
169 : // in a row. consec_zero_mv[] defined for 8x8 blocks, so consider all
170 : // 4 sub-blocks for 16x16 block. Also, avoid skin blocks.
171 0 : int consec_zeromv =
172 0 : VPXMIN(cpi->consec_zero_mv[bl_index],
173 : VPXMIN(cpi->consec_zero_mv[bl_index1],
174 : VPXMIN(cpi->consec_zero_mv[bl_index2],
175 : cpi->consec_zero_mv[bl_index3])));
176 0 : int is_skin = 0;
177 0 : if (cpi->use_skin_detection) {
178 0 : is_skin =
179 : vp9_compute_skin_block(src_y, src_u, src_v, src_ystride,
180 : src_uvstride, bsize, consec_zeromv, 0);
181 : }
182 0 : if (frame_low_motion &&
183 0 : cpi->consec_zero_mv[bl_index] > thresh_consec_zeromv &&
184 0 : cpi->consec_zero_mv[bl_index1] > thresh_consec_zeromv &&
185 0 : cpi->consec_zero_mv[bl_index2] > thresh_consec_zeromv &&
186 0 : cpi->consec_zero_mv[bl_index3] > thresh_consec_zeromv &&
187 : !is_skin) {
188 : // Compute variance.
189 : unsigned int sse;
190 0 : unsigned int variance = cpi->fn_ptr[bsize].vf(
191 : src_y, src_ystride, last_src_y, last_src_ystride, &sse);
192 : // Only consider this block as valid for noise measurement if the
193 : // average term (sse - variance = N * avg^{2}, N = 16X16) of the
194 : // temporal residual is small (avoid effects from lighting change).
195 0 : if ((sse - variance) < thresh_sum_diff) {
196 : unsigned int sse2;
197 0 : const unsigned int spatial_variance = cpi->fn_ptr[bsize].vf(
198 : src_y, src_ystride, const_source, 0, &sse2);
199 : // Avoid blocks with high brightness and high spatial variance.
200 0 : if ((sse2 - spatial_variance) < thresh_sum_spatial &&
201 : spatial_variance < thresh_spatial_var) {
202 0 : avg_est += variance / ((spatial_variance >> 9) + 1);
203 0 : num_samples++;
204 : }
205 : }
206 : }
207 : }
208 0 : src_y += 8;
209 0 : last_src_y += 8;
210 0 : src_u += 4;
211 0 : src_v += 4;
212 : }
213 0 : src_y += (src_ystride << 3) - (cm->mi_cols << 3);
214 0 : last_src_y += (last_src_ystride << 3) - (cm->mi_cols << 3);
215 0 : src_u += (src_uvstride << 2) - (cm->mi_cols << 2);
216 0 : src_v += (src_uvstride << 2) - (cm->mi_cols << 2);
217 : }
218 0 : ne->last_w = cm->width;
219 0 : ne->last_h = cm->height;
220 : // Update noise estimate if we have at a minimum number of block samples,
221 : // and avg_est > 0 (avg_est == 0 can happen if the application inputs
222 : // duplicate frames).
223 0 : if (num_samples > min_blocks_estimate && avg_est > 0) {
224 : // Normalize.
225 0 : avg_est = avg_est / num_samples;
226 : // Update noise estimate.
227 0 : ne->value = (int)((15 * ne->value + avg_est) >> 4);
228 0 : ne->count++;
229 0 : if (ne->count == ne->num_frames_estimate) {
230 : // Reset counter and check noise level condition.
231 0 : ne->num_frames_estimate = 30;
232 0 : ne->count = 0;
233 0 : ne->level = vp9_noise_estimate_extract_level(ne);
234 : #if CONFIG_VP9_TEMPORAL_DENOISING
235 : if (cpi->oxcf.noise_sensitivity > 0)
236 : vp9_denoiser_set_noise_level(&cpi->denoiser, ne->level);
237 : #endif
238 : }
239 : }
240 : }
241 : #if CONFIG_VP9_TEMPORAL_DENOISING
242 : if (cpi->oxcf.noise_sensitivity > 0)
243 : copy_frame(&cpi->denoiser.last_source, cpi->Source);
244 : #endif
245 : }
|