/src/libvpx/vp9/encoder/vp9_noise_estimate.c
Line | Count | Source (jump to first uncovered line) |
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 | | #if CONFIG_VP9_TEMPORAL_DENOISING |
25 | | // For SVC: only do noise estimation on top spatial layer. |
26 | | static INLINE int noise_est_svc(const struct VP9_COMP *const cpi) { |
27 | | return (!cpi->use_svc || |
28 | | (cpi->use_svc && |
29 | | cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1)); |
30 | | } |
31 | | #endif |
32 | | |
33 | 2.98k | void vp9_noise_estimate_init(NOISE_ESTIMATE *const ne, int width, int height) { |
34 | 2.98k | ne->enabled = 0; |
35 | 2.98k | ne->level = (width * height < 1280 * 720) ? kLowLow : kLow; |
36 | 2.98k | ne->value = 0; |
37 | 2.98k | ne->count = 0; |
38 | 2.98k | ne->thresh = 90; |
39 | 2.98k | ne->last_w = 0; |
40 | 2.98k | ne->last_h = 0; |
41 | 2.98k | if (width * height >= 1920 * 1080) { |
42 | 0 | ne->thresh = 200; |
43 | 2.98k | } else if (width * height >= 1280 * 720) { |
44 | 0 | ne->thresh = 140; |
45 | 2.98k | } else if (width * height >= 640 * 360) { |
46 | 37 | ne->thresh = 115; |
47 | 37 | } |
48 | 2.98k | ne->num_frames_estimate = 15; |
49 | 2.98k | ne->adapt_thresh = (3 * ne->thresh) >> 1; |
50 | 2.98k | } |
51 | | |
52 | 42.2k | static int enable_noise_estimation(VP9_COMP *const cpi) { |
53 | 42.2k | #if CONFIG_VP9_HIGHBITDEPTH |
54 | 42.2k | if (cpi->common.use_highbitdepth) return 0; |
55 | 42.2k | #endif |
56 | | // Enable noise estimation if denoising is on. |
57 | | #if CONFIG_VP9_TEMPORAL_DENOISING |
58 | | if (cpi->oxcf.noise_sensitivity > 0 && noise_est_svc(cpi) && |
59 | | cpi->common.width >= 320 && cpi->common.height >= 180) |
60 | | return 1; |
61 | | #endif |
62 | | // Only allow noise estimate under certain encoding mode. |
63 | | // Enabled for 1 pass CBR, speed >=5, and if resolution is same as original. |
64 | | // Not enabled for SVC mode and screen_content_mode. |
65 | | // Not enabled for low resolutions. |
66 | 42.2k | if (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == VPX_CBR && |
67 | 42.2k | cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cpi->oxcf.speed >= 5 && |
68 | 42.2k | cpi->resize_state == ORIG && cpi->resize_pending == 0 && !cpi->use_svc && |
69 | 42.2k | cpi->oxcf.content != VP9E_CONTENT_SCREEN && |
70 | 42.2k | cpi->common.width * cpi->common.height >= 640 * 360) |
71 | 0 | return 1; |
72 | 42.2k | else |
73 | 42.2k | return 0; |
74 | 42.2k | } |
75 | | |
76 | | #if CONFIG_VP9_TEMPORAL_DENOISING |
77 | | static void copy_frame(YV12_BUFFER_CONFIG *const dest, |
78 | | const YV12_BUFFER_CONFIG *const src) { |
79 | | int r; |
80 | | const uint8_t *srcbuf = src->y_buffer; |
81 | | uint8_t *destbuf = dest->y_buffer; |
82 | | |
83 | | assert(dest->y_width == src->y_width); |
84 | | assert(dest->y_height == src->y_height); |
85 | | |
86 | | for (r = 0; r < dest->y_height; ++r) { |
87 | | memcpy(destbuf, srcbuf, dest->y_width); |
88 | | destbuf += dest->y_stride; |
89 | | srcbuf += src->y_stride; |
90 | | } |
91 | | } |
92 | | #endif // CONFIG_VP9_TEMPORAL_DENOISING |
93 | | |
94 | 0 | NOISE_LEVEL vp9_noise_estimate_extract_level(NOISE_ESTIMATE *const ne) { |
95 | 0 | int noise_level = kLowLow; |
96 | 0 | if (ne->value > (ne->thresh << 1)) { |
97 | 0 | noise_level = kHigh; |
98 | 0 | } else { |
99 | 0 | if (ne->value > ne->thresh) |
100 | 0 | noise_level = kMedium; |
101 | 0 | else if (ne->value > (ne->thresh >> 1)) |
102 | 0 | noise_level = kLow; |
103 | 0 | else |
104 | 0 | noise_level = kLowLow; |
105 | 0 | } |
106 | 0 | return noise_level; |
107 | 0 | } |
108 | | |
109 | 42.2k | void vp9_update_noise_estimate(VP9_COMP *const cpi) { |
110 | 42.2k | const VP9_COMMON *const cm = &cpi->common; |
111 | 42.2k | NOISE_ESTIMATE *const ne = &cpi->noise_estimate; |
112 | 42.2k | const int low_res = (cm->width <= 352 && cm->height <= 288); |
113 | | // Estimate of noise level every frame_period frames. |
114 | 42.2k | int frame_period = 8; |
115 | 42.2k | int thresh_consec_zeromv = 6; |
116 | 42.2k | int frame_counter = cm->current_video_frame; |
117 | | // Estimate is between current source and last source. |
118 | 42.2k | YV12_BUFFER_CONFIG *last_source = cpi->Last_Source; |
119 | | #if CONFIG_VP9_TEMPORAL_DENOISING |
120 | | if (cpi->oxcf.noise_sensitivity > 0 && noise_est_svc(cpi)) { |
121 | | last_source = &cpi->denoiser.last_source; |
122 | | // Tune these thresholds for different resolutions when denoising is |
123 | | // enabled. |
124 | | if (cm->width > 640 && cm->width <= 1920) { |
125 | | thresh_consec_zeromv = 2; |
126 | | } |
127 | | } |
128 | | #endif |
129 | 42.2k | ne->enabled = enable_noise_estimation(cpi); |
130 | 42.2k | if (cpi->svc.number_spatial_layers > 1) |
131 | 0 | frame_counter = cpi->svc.current_superframe; |
132 | 42.2k | if (!ne->enabled || frame_counter % frame_period != 0 || |
133 | 42.2k | last_source == NULL || |
134 | 42.2k | (cpi->svc.number_spatial_layers == 1 && |
135 | 42.2k | (ne->last_w != cm->width || ne->last_h != cm->height))) { |
136 | | #if CONFIG_VP9_TEMPORAL_DENOISING |
137 | | if (cpi->oxcf.noise_sensitivity > 0 && noise_est_svc(cpi)) |
138 | | copy_frame(&cpi->denoiser.last_source, cpi->Source); |
139 | | #endif |
140 | 42.2k | if (last_source != NULL) { |
141 | 20.9k | ne->last_w = cm->width; |
142 | 20.9k | ne->last_h = cm->height; |
143 | 20.9k | } |
144 | 42.2k | return; |
145 | 42.2k | } else if (frame_counter > 60 && cpi->svc.num_encoded_top_layer > 1 && |
146 | 0 | cpi->rc.frames_since_key > cpi->svc.number_spatial_layers && |
147 | 0 | cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1 && |
148 | 0 | cpi->rc.avg_frame_low_motion < (low_res ? 60 : 40)) { |
149 | | // Force noise estimation to 0 and denoiser off if content has high motion. |
150 | 0 | ne->level = kLowLow; |
151 | 0 | ne->count = 0; |
152 | 0 | ne->num_frames_estimate = 10; |
153 | | #if CONFIG_VP9_TEMPORAL_DENOISING |
154 | | if (cpi->oxcf.noise_sensitivity > 0 && noise_est_svc(cpi) && |
155 | | cpi->svc.current_superframe > 1) { |
156 | | vp9_denoiser_set_noise_level(cpi, ne->level); |
157 | | copy_frame(&cpi->denoiser.last_source, cpi->Source); |
158 | | } |
159 | | #endif |
160 | 0 | return; |
161 | 0 | } else { |
162 | 0 | unsigned int bin_size = 100; |
163 | 0 | unsigned int hist[MAX_VAR_HIST_BINS] = { 0 }; |
164 | 0 | unsigned int hist_avg[MAX_VAR_HIST_BINS]; |
165 | 0 | unsigned int max_bin = 0; |
166 | 0 | unsigned int max_bin_count = 0; |
167 | 0 | unsigned int bin_cnt; |
168 | 0 | int bsize = BLOCK_16X16; |
169 | | // Loop over sub-sample of 16x16 blocks of frame, and for blocks that have |
170 | | // been encoded as zero/small mv at least x consecutive frames, compute |
171 | | // the variance to update estimate of noise in the source. |
172 | 0 | const uint8_t *src_y = cpi->Source->y_buffer; |
173 | 0 | const int src_ystride = cpi->Source->y_stride; |
174 | 0 | const uint8_t *last_src_y = last_source->y_buffer; |
175 | 0 | const int last_src_ystride = last_source->y_stride; |
176 | 0 | const uint8_t *src_u = cpi->Source->u_buffer; |
177 | 0 | const uint8_t *src_v = cpi->Source->v_buffer; |
178 | 0 | const int src_uvstride = cpi->Source->uv_stride; |
179 | 0 | int mi_row, mi_col; |
180 | 0 | int num_low_motion = 0; |
181 | 0 | int frame_low_motion = 1; |
182 | 0 | for (mi_row = 0; mi_row < cm->mi_rows; mi_row++) { |
183 | 0 | for (mi_col = 0; mi_col < cm->mi_cols; mi_col++) { |
184 | 0 | int bl_index = mi_row * cm->mi_cols + mi_col; |
185 | 0 | if (cpi->consec_zero_mv[bl_index] > thresh_consec_zeromv) |
186 | 0 | num_low_motion++; |
187 | 0 | } |
188 | 0 | } |
189 | 0 | if (num_low_motion < ((3 * cm->mi_rows * cm->mi_cols) >> 3)) |
190 | 0 | frame_low_motion = 0; |
191 | 0 | for (mi_row = 0; mi_row < cm->mi_rows; mi_row++) { |
192 | 0 | for (mi_col = 0; mi_col < cm->mi_cols; mi_col++) { |
193 | | // 16x16 blocks, 1/4 sample of frame. |
194 | 0 | if (mi_row % 4 == 0 && mi_col % 4 == 0 && mi_row < cm->mi_rows - 1 && |
195 | 0 | mi_col < cm->mi_cols - 1) { |
196 | 0 | int bl_index = mi_row * cm->mi_cols + mi_col; |
197 | 0 | int bl_index1 = bl_index + 1; |
198 | 0 | int bl_index2 = bl_index + cm->mi_cols; |
199 | 0 | int bl_index3 = bl_index2 + 1; |
200 | 0 | int consec_zeromv = |
201 | 0 | VPXMIN(cpi->consec_zero_mv[bl_index], |
202 | 0 | VPXMIN(cpi->consec_zero_mv[bl_index1], |
203 | 0 | VPXMIN(cpi->consec_zero_mv[bl_index2], |
204 | 0 | cpi->consec_zero_mv[bl_index3]))); |
205 | | // Only consider blocks that are likely steady background. i.e., have |
206 | | // been encoded as zero/low motion x (= thresh_consec_zeromv) frames |
207 | | // in a row. consec_zero_mv[] defined for 8x8 blocks, so consider all |
208 | | // 4 sub-blocks for 16x16 block. And exclude this frame if |
209 | | // high_source_sad is true (i.e., scene/content change). |
210 | 0 | if (frame_low_motion && consec_zeromv > thresh_consec_zeromv && |
211 | 0 | !cpi->rc.high_source_sad && |
212 | 0 | !cpi->svc.high_source_sad_superframe) { |
213 | 0 | int is_skin = 0; |
214 | 0 | if (cpi->use_skin_detection) { |
215 | 0 | is_skin = |
216 | 0 | vp9_compute_skin_block(src_y, src_u, src_v, src_ystride, |
217 | 0 | src_uvstride, bsize, consec_zeromv, 0); |
218 | 0 | } |
219 | 0 | if (!is_skin) { |
220 | 0 | unsigned int sse; |
221 | | // Compute variance between co-located blocks from current and |
222 | | // last input frames. |
223 | 0 | unsigned int variance = cpi->fn_ptr[bsize].vf( |
224 | 0 | src_y, src_ystride, last_src_y, last_src_ystride, &sse); |
225 | 0 | unsigned int hist_index = variance / bin_size; |
226 | 0 | if (hist_index < MAX_VAR_HIST_BINS) |
227 | 0 | hist[hist_index]++; |
228 | 0 | else if (hist_index < 3 * (MAX_VAR_HIST_BINS >> 1)) |
229 | 0 | hist[MAX_VAR_HIST_BINS - 1]++; // Account for the tail |
230 | 0 | } |
231 | 0 | } |
232 | 0 | } |
233 | 0 | src_y += 8; |
234 | 0 | last_src_y += 8; |
235 | 0 | src_u += 4; |
236 | 0 | src_v += 4; |
237 | 0 | } |
238 | 0 | src_y += (src_ystride << 3) - (cm->mi_cols << 3); |
239 | 0 | last_src_y += (last_src_ystride << 3) - (cm->mi_cols << 3); |
240 | 0 | src_u += (src_uvstride << 2) - (cm->mi_cols << 2); |
241 | 0 | src_v += (src_uvstride << 2) - (cm->mi_cols << 2); |
242 | 0 | } |
243 | 0 | ne->last_w = cm->width; |
244 | 0 | ne->last_h = cm->height; |
245 | | // Adjust histogram to account for effect that histogram flattens |
246 | | // and shifts to zero as scene darkens. |
247 | 0 | if (hist[0] > 10 && (hist[MAX_VAR_HIST_BINS - 1] > hist[0] >> 2)) { |
248 | 0 | hist[0] = 0; |
249 | 0 | hist[1] >>= 2; |
250 | 0 | hist[2] >>= 2; |
251 | 0 | hist[3] >>= 2; |
252 | 0 | hist[4] >>= 1; |
253 | 0 | hist[5] >>= 1; |
254 | 0 | hist[6] = 3 * hist[6] >> 1; |
255 | 0 | hist[MAX_VAR_HIST_BINS - 1] >>= 1; |
256 | 0 | } |
257 | | |
258 | | // Average hist[] and find largest bin |
259 | 0 | for (bin_cnt = 0; bin_cnt < MAX_VAR_HIST_BINS; bin_cnt++) { |
260 | 0 | if (bin_cnt == 0) |
261 | 0 | hist_avg[bin_cnt] = (hist[0] + hist[1] + hist[2]) / 3; |
262 | 0 | else if (bin_cnt == MAX_VAR_HIST_BINS - 1) |
263 | 0 | hist_avg[bin_cnt] = hist[MAX_VAR_HIST_BINS - 1] >> 2; |
264 | 0 | else if (bin_cnt == MAX_VAR_HIST_BINS - 2) |
265 | 0 | hist_avg[bin_cnt] = (hist[bin_cnt - 1] + 2 * hist[bin_cnt] + |
266 | 0 | (hist[bin_cnt + 1] >> 1) + 2) >> |
267 | 0 | 2; |
268 | 0 | else |
269 | 0 | hist_avg[bin_cnt] = |
270 | 0 | (hist[bin_cnt - 1] + 2 * hist[bin_cnt] + hist[bin_cnt + 1] + 2) >> |
271 | 0 | 2; |
272 | |
|
273 | 0 | if (hist_avg[bin_cnt] > max_bin_count) { |
274 | 0 | max_bin_count = hist_avg[bin_cnt]; |
275 | 0 | max_bin = bin_cnt; |
276 | 0 | } |
277 | 0 | } |
278 | | |
279 | | // Scale by 40 to work with existing thresholds |
280 | 0 | ne->value = (int)((3 * ne->value + max_bin * 40) >> 2); |
281 | | // Quickly increase VNR strength when the noise level increases suddenly. |
282 | 0 | if (ne->level < kMedium && ne->value > ne->adapt_thresh) { |
283 | 0 | ne->count = ne->num_frames_estimate; |
284 | 0 | } else { |
285 | 0 | ne->count++; |
286 | 0 | } |
287 | 0 | if (ne->count == ne->num_frames_estimate) { |
288 | | // Reset counter and check noise level condition. |
289 | 0 | ne->num_frames_estimate = 30; |
290 | 0 | ne->count = 0; |
291 | 0 | ne->level = vp9_noise_estimate_extract_level(ne); |
292 | | #if CONFIG_VP9_TEMPORAL_DENOISING |
293 | | if (cpi->oxcf.noise_sensitivity > 0 && noise_est_svc(cpi)) |
294 | | vp9_denoiser_set_noise_level(cpi, ne->level); |
295 | | #endif |
296 | 0 | } |
297 | 0 | } |
298 | | #if CONFIG_VP9_TEMPORAL_DENOISING |
299 | | if (cpi->oxcf.noise_sensitivity > 0 && noise_est_svc(cpi)) |
300 | | copy_frame(&cpi->denoiser.last_source, cpi->Source); |
301 | | #endif |
302 | 42.2k | } |