2 * Copyright (c) 2015 The WebM project authors. All Rights Reserved.
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.
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"
24 void vp9_noise_estimate_init(NOISE_ESTIMATE *const ne,
34 if (width * height >= 1920 * 1080) {
36 } else if (width * height >= 1280 * 720) {
39 ne->num_frames_estimate = 20;
42 int enable_noise_estimation(VP9_COMP *const cpi) {
43 // Enable noise estimation if denoising is on (and cyclic refresh, since
44 // noise estimate is currently using a struct defined in cyclic refresh).
45 #if CONFIG_VP9_TEMPORAL_DENOISING
46 if (cpi->oxcf.noise_sensitivity > 0 &&
47 cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
50 // Only allow noise estimate under certain encoding mode.
51 // Enabled for 1 pass CBR, speed >=5, and if resolution is same as original.
52 // Not enabled for SVC mode and screen_content_mode.
53 // Not enabled for low resolutions.
54 if (cpi->oxcf.pass == 0 &&
55 cpi->oxcf.rc_mode == VPX_CBR &&
56 cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ &&
57 cpi->oxcf.speed >= 5 &&
58 cpi->resize_state == ORIG &&
59 cpi->resize_pending == 0 &&
61 cpi->oxcf.content != VP9E_CONTENT_SCREEN &&
62 cpi->common.width >= 640 &&
63 cpi->common.height >= 480)
69 static void copy_frame(YV12_BUFFER_CONFIG * const dest,
70 const YV12_BUFFER_CONFIG * const src) {
72 const uint8_t *srcbuf = src->y_buffer;
73 uint8_t *destbuf = dest->y_buffer;
75 assert(dest->y_width == src->y_width);
76 assert(dest->y_height == src->y_height);
78 for (r = 0; r < dest->y_height; ++r) {
79 memcpy(destbuf, srcbuf, dest->y_width);
80 destbuf += dest->y_stride;
81 srcbuf += src->y_stride;
85 NOISE_LEVEL vp9_noise_estimate_extract_level(NOISE_ESTIMATE *const ne) {
86 int noise_level = kLowLow;
87 if (ne->value > (ne->thresh << 1)) {
90 if (ne->value > ne->thresh)
91 noise_level = kMedium;
92 else if (ne->value > (ne->thresh >> 1))
95 noise_level = kLowLow;
100 void vp9_update_noise_estimate(VP9_COMP *const cpi) {
101 const VP9_COMMON *const cm = &cpi->common;
102 CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
103 NOISE_ESTIMATE *const ne = &cpi->noise_estimate;
104 // Estimate of noise level every frame_period frames.
105 int frame_period = 10;
106 int thresh_consec_zeromv = 8;
107 unsigned int thresh_sum_diff = 100;
108 unsigned int thresh_sum_spatial = (200 * 200) << 8;
109 unsigned int thresh_spatial_var = (32 * 32) << 8;
110 int min_blocks_estimate = cm->mi_rows * cm->mi_cols >> 7;
111 // Estimate is between current source and last source.
112 YV12_BUFFER_CONFIG *last_source = cpi->Last_Source;
113 #if CONFIG_VP9_TEMPORAL_DENOISING
114 if (cpi->oxcf.noise_sensitivity > 0)
115 last_source = &cpi->denoiser.last_source;
117 ne->enabled = enable_noise_estimation(cpi);
119 cm->current_video_frame % frame_period != 0 ||
120 last_source == NULL ||
121 ne->last_w != cm->width ||
122 ne->last_h != cm->height) {
123 #if CONFIG_VP9_TEMPORAL_DENOISING
124 if (cpi->oxcf.noise_sensitivity > 0)
125 copy_frame(&cpi->denoiser.last_source, cpi->Source);
127 if (last_source != NULL) {
128 ne->last_w = cm->width;
129 ne->last_h = cm->height;
134 uint64_t avg_est = 0;
135 int bsize = BLOCK_16X16;
136 static const unsigned char const_source[16] = {
137 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
138 // Loop over sub-sample of 16x16 blocks of frame, and for blocks that have
139 // been encoded as zero/small mv at least x consecutive frames, compute
140 // the variance to update estimate of noise in the source.
141 const uint8_t *src_y = cpi->Source->y_buffer;
142 const int src_ystride = cpi->Source->y_stride;
143 const uint8_t *last_src_y = last_source->y_buffer;
144 const int last_src_ystride = last_source->y_stride;
145 const uint8_t *src_u = cpi->Source->u_buffer;
146 const uint8_t *src_v = cpi->Source->v_buffer;
147 const int src_uvstride = cpi->Source->uv_stride;
148 const int y_width_shift = (4 << b_width_log2_lookup[bsize]) >> 1;
149 const int y_height_shift = (4 << b_height_log2_lookup[bsize]) >> 1;
150 const int uv_width_shift = y_width_shift >> 1;
151 const int uv_height_shift = y_height_shift >> 1;
153 int num_low_motion = 0;
154 int frame_low_motion = 1;
155 for (mi_row = 0; mi_row < cm->mi_rows; mi_row++) {
156 for (mi_col = 0; mi_col < cm->mi_cols; mi_col++) {
157 int bl_index = mi_row * cm->mi_cols + mi_col;
158 if (cr->consec_zero_mv[bl_index] > thresh_consec_zeromv)
162 if (num_low_motion < ((3 * cm->mi_rows * cm->mi_cols) >> 3))
163 frame_low_motion = 0;
164 for (mi_row = 0; mi_row < cm->mi_rows; mi_row++) {
165 for (mi_col = 0; mi_col < cm->mi_cols; mi_col++) {
166 // 16x16 blocks, 1/4 sample of frame.
167 if (mi_row % 4 == 0 && mi_col % 4 == 0) {
168 int bl_index = mi_row * cm->mi_cols + mi_col;
169 int bl_index1 = bl_index + 1;
170 int bl_index2 = bl_index + cm->mi_cols;
171 int bl_index3 = bl_index2 + 1;
172 // Only consider blocks that are likely steady background. i.e, have
173 // been encoded as zero/low motion x (= thresh_consec_zeromv) frames
174 // in a row. consec_zero_mv[] defined for 8x8 blocks, so consider all
175 // 4 sub-blocks for 16x16 block. Also, avoid skin blocks.
176 const uint8_t ysource =
177 src_y[y_height_shift * src_ystride + y_width_shift];
178 const uint8_t usource =
179 src_u[uv_height_shift * src_uvstride + uv_width_shift];
180 const uint8_t vsource =
181 src_v[uv_height_shift * src_uvstride + uv_width_shift];
182 int is_skin = vp9_skin_pixel(ysource, usource, vsource);
183 if (frame_low_motion &&
184 cr->consec_zero_mv[bl_index] > thresh_consec_zeromv &&
185 cr->consec_zero_mv[bl_index1] > thresh_consec_zeromv &&
186 cr->consec_zero_mv[bl_index2] > thresh_consec_zeromv &&
187 cr->consec_zero_mv[bl_index3] > thresh_consec_zeromv &&
191 unsigned int variance = cpi->fn_ptr[bsize].vf(src_y,
196 // Only consider this block as valid for noise measurement if the
197 // average term (sse - variance = N * avg^{2}, N = 16X16) of the
198 // temporal residual is small (avoid effects from lighting change).
199 if ((sse - variance) < thresh_sum_diff) {
201 const unsigned int spatial_variance =
202 cpi->fn_ptr[bsize].vf(src_y, src_ystride, const_source,
204 // Avoid blocks with high brightness and high spatial variance.
205 if ((sse2 - spatial_variance) < thresh_sum_spatial &&
206 spatial_variance < thresh_spatial_var) {
207 avg_est += variance / ((spatial_variance >> 9) + 1);
218 src_y += (src_ystride << 3) - (cm->mi_cols << 3);
219 last_src_y += (last_src_ystride << 3) - (cm->mi_cols << 3);
220 src_u += (src_uvstride << 2) - (cm->mi_cols << 2);
221 src_v += (src_uvstride << 2) - (cm->mi_cols << 2);
223 ne->last_w = cm->width;
224 ne->last_h = cm->height;
225 // Update noise estimate if we have at a minimum number of block samples,
226 // and avg_est > 0 (avg_est == 0 can happen if the application inputs
227 // duplicate frames).
228 if (num_samples > min_blocks_estimate && avg_est > 0) {
230 avg_est = avg_est / num_samples;
231 // Update noise estimate.
232 ne->value = (int)((15 * ne->value + avg_est) >> 4);
234 if (ne->count == ne->num_frames_estimate) {
235 // Reset counter and check noise level condition.
236 ne->num_frames_estimate = 30;
238 ne->level = vp9_noise_estimate_extract_level(ne);
239 #if CONFIG_VP9_TEMPORAL_DENOISING
240 if (cpi->oxcf.noise_sensitivity > 0)
241 vp9_denoiser_set_noise_level(&cpi->denoiser, ne->level);
246 #if CONFIG_VP9_TEMPORAL_DENOISING
247 if (cpi->oxcf.noise_sensitivity > 0)
248 copy_frame(&cpi->denoiser.last_source, cpi->Source);