• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
vp9_noise_estimate_init(NOISE_ESTIMATE * const ne,int width,int height)24 void vp9_noise_estimate_init(NOISE_ESTIMATE *const ne, int width, int height) {
25   ne->enabled = 0;
26   ne->level = kLowLow;
27   ne->value = 0;
28   ne->count = 0;
29   ne->thresh = 90;
30   ne->last_w = 0;
31   ne->last_h = 0;
32   if (width * height >= 1920 * 1080) {
33     ne->thresh = 200;
34   } else if (width * height >= 1280 * 720) {
35     ne->thresh = 140;
36   } else if (width * height >= 640 * 360) {
37     ne->thresh = 100;
38   }
39   ne->num_frames_estimate = 15;
40 }
41 
enable_noise_estimation(VP9_COMP * const cpi)42 static int enable_noise_estimation(VP9_COMP *const cpi) {
43 #if CONFIG_VP9_HIGHBITDEPTH
44   if (cpi->common.use_highbitdepth) return 0;
45 #endif
46 // Enable noise estimation if denoising is on.
47 #if CONFIG_VP9_TEMPORAL_DENOISING
48   if (cpi->oxcf.noise_sensitivity > 0 && denoise_svc(cpi) &&
49       cpi->common.width >= 320 && cpi->common.height >= 180)
50     return 1;
51 #endif
52   // Only allow noise estimate under certain encoding mode.
53   // Enabled for 1 pass CBR, speed >=5, and if resolution is same as original.
54   // Not enabled for SVC mode and screen_content_mode.
55   // Not enabled for low resolutions.
56   if (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == VPX_CBR &&
57       cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cpi->oxcf.speed >= 5 &&
58       cpi->resize_state == ORIG && cpi->resize_pending == 0 && !cpi->use_svc &&
59       cpi->oxcf.content != VP9E_CONTENT_SCREEN && cpi->common.width >= 640 &&
60       cpi->common.height >= 360)
61     return 1;
62   else
63     return 0;
64 }
65 
66 #if CONFIG_VP9_TEMPORAL_DENOISING
copy_frame(YV12_BUFFER_CONFIG * const dest,const YV12_BUFFER_CONFIG * const src)67 static void copy_frame(YV12_BUFFER_CONFIG *const dest,
68                        const YV12_BUFFER_CONFIG *const src) {
69   int r;
70   const uint8_t *srcbuf = src->y_buffer;
71   uint8_t *destbuf = dest->y_buffer;
72 
73   assert(dest->y_width == src->y_width);
74   assert(dest->y_height == src->y_height);
75 
76   for (r = 0; r < dest->y_height; ++r) {
77     memcpy(destbuf, srcbuf, dest->y_width);
78     destbuf += dest->y_stride;
79     srcbuf += src->y_stride;
80   }
81 }
82 #endif  // CONFIG_VP9_TEMPORAL_DENOISING
83 
vp9_noise_estimate_extract_level(NOISE_ESTIMATE * const ne)84 NOISE_LEVEL vp9_noise_estimate_extract_level(NOISE_ESTIMATE *const ne) {
85   int noise_level = kLowLow;
86   if (ne->value > (ne->thresh << 1)) {
87     noise_level = kHigh;
88   } else {
89     if (ne->value > ne->thresh)
90       noise_level = kMedium;
91     else if (ne->value > ((9 * ne->thresh) >> 4))
92       noise_level = kLow;
93     else
94       noise_level = kLowLow;
95   }
96   return noise_level;
97 }
98 
vp9_update_noise_estimate(VP9_COMP * const cpi)99 void vp9_update_noise_estimate(VP9_COMP *const cpi) {
100   const VP9_COMMON *const cm = &cpi->common;
101   NOISE_ESTIMATE *const ne = &cpi->noise_estimate;
102   const int low_res = (cm->width <= 352 && cm->height <= 288);
103   // Estimate of noise level every frame_period frames.
104   int frame_period = 8;
105   int thresh_consec_zeromv = 6;
106   unsigned int thresh_sum_diff = 100;
107   unsigned int thresh_sum_spatial = (200 * 200) << 8;
108   unsigned int thresh_spatial_var = (32 * 32) << 8;
109   int min_blocks_estimate = cm->mi_rows * cm->mi_cols >> 7;
110   int frame_counter = cm->current_video_frame;
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 && denoise_svc(cpi)) {
115     last_source = &cpi->denoiser.last_source;
116     // Tune these thresholds for different resolutions when denoising is
117     // enabled.
118     if (cm->width > 640 && cm->width < 1920) {
119       thresh_consec_zeromv = 4;
120       thresh_sum_diff = 200;
121       thresh_sum_spatial = (120 * 120) << 8;
122       thresh_spatial_var = (48 * 48) << 8;
123     }
124   }
125 #endif
126   ne->enabled = enable_noise_estimation(cpi);
127   if (cpi->svc.number_spatial_layers > 1)
128     frame_counter = cpi->svc.current_superframe;
129   if (!ne->enabled || frame_counter % frame_period != 0 ||
130       last_source == NULL ||
131       (cpi->svc.number_spatial_layers == 1 &&
132        (ne->last_w != cm->width || ne->last_h != cm->height))) {
133 #if CONFIG_VP9_TEMPORAL_DENOISING
134     if (cpi->oxcf.noise_sensitivity > 0 && denoise_svc(cpi))
135       copy_frame(&cpi->denoiser.last_source, cpi->Source);
136 #endif
137     if (last_source != NULL) {
138       ne->last_w = cm->width;
139       ne->last_h = cm->height;
140     }
141     return;
142   } else if (cm->current_video_frame > 60 &&
143              cpi->rc.avg_frame_low_motion < (low_res ? 70 : 50)) {
144     // Force noise estimation to 0 and denoiser off if content has high motion.
145     ne->level = kLowLow;
146     ne->count = 0;
147     ne->num_frames_estimate = 10;
148 #if CONFIG_VP9_TEMPORAL_DENOISING
149     if (cpi->oxcf.noise_sensitivity > 0 && denoise_svc(cpi) &&
150         cpi->svc.current_superframe > 1) {
151       vp9_denoiser_set_noise_level(&cpi->denoiser, ne->level);
152       copy_frame(&cpi->denoiser.last_source, cpi->Source);
153     }
154 #endif
155     return;
156   } else {
157     int num_samples = 0;
158     uint64_t avg_est = 0;
159     int bsize = BLOCK_16X16;
160     static const unsigned char const_source[16] = { 0, 0, 0, 0, 0, 0, 0, 0,
161                                                     0, 0, 0, 0, 0, 0, 0, 0 };
162     // Loop over sub-sample of 16x16 blocks of frame, and for blocks that have
163     // been encoded as zero/small mv at least x consecutive frames, compute
164     // the variance to update estimate of noise in the source.
165     const uint8_t *src_y = cpi->Source->y_buffer;
166     const int src_ystride = cpi->Source->y_stride;
167     const uint8_t *last_src_y = last_source->y_buffer;
168     const int last_src_ystride = last_source->y_stride;
169     const uint8_t *src_u = cpi->Source->u_buffer;
170     const uint8_t *src_v = cpi->Source->v_buffer;
171     const int src_uvstride = cpi->Source->uv_stride;
172     int mi_row, mi_col;
173     int num_low_motion = 0;
174     int frame_low_motion = 1;
175     for (mi_row = 0; mi_row < cm->mi_rows; mi_row++) {
176       for (mi_col = 0; mi_col < cm->mi_cols; mi_col++) {
177         int bl_index = mi_row * cm->mi_cols + mi_col;
178         if (cpi->consec_zero_mv[bl_index] > thresh_consec_zeromv)
179           num_low_motion++;
180       }
181     }
182     if (num_low_motion < ((3 * cm->mi_rows * cm->mi_cols) >> 3))
183       frame_low_motion = 0;
184     for (mi_row = 0; mi_row < cm->mi_rows; mi_row++) {
185       for (mi_col = 0; mi_col < cm->mi_cols; mi_col++) {
186         // 16x16 blocks, 1/4 sample of frame.
187         if (mi_row % 4 == 0 && mi_col % 4 == 0 && mi_row < cm->mi_rows - 1 &&
188             mi_col < cm->mi_cols - 1) {
189           int bl_index = mi_row * cm->mi_cols + mi_col;
190           int bl_index1 = bl_index + 1;
191           int bl_index2 = bl_index + cm->mi_cols;
192           int bl_index3 = bl_index2 + 1;
193           // Only consider blocks that are likely steady background. i.e, have
194           // been encoded as zero/low motion x (= thresh_consec_zeromv) frames
195           // in a row. consec_zero_mv[] defined for 8x8 blocks, so consider all
196           // 4 sub-blocks for 16x16 block. Also, avoid skin blocks.
197           int consec_zeromv =
198               VPXMIN(cpi->consec_zero_mv[bl_index],
199                      VPXMIN(cpi->consec_zero_mv[bl_index1],
200                             VPXMIN(cpi->consec_zero_mv[bl_index2],
201                                    cpi->consec_zero_mv[bl_index3])));
202           int is_skin = 0;
203           if (cpi->use_skin_detection) {
204             is_skin =
205                 vp9_compute_skin_block(src_y, src_u, src_v, src_ystride,
206                                        src_uvstride, bsize, consec_zeromv, 0);
207           }
208           if (frame_low_motion &&
209               cpi->consec_zero_mv[bl_index] > thresh_consec_zeromv &&
210               cpi->consec_zero_mv[bl_index1] > thresh_consec_zeromv &&
211               cpi->consec_zero_mv[bl_index2] > thresh_consec_zeromv &&
212               cpi->consec_zero_mv[bl_index3] > thresh_consec_zeromv &&
213               !is_skin) {
214             // Compute variance.
215             unsigned int sse;
216             unsigned int variance = cpi->fn_ptr[bsize].vf(
217                 src_y, src_ystride, last_src_y, last_src_ystride, &sse);
218             // Only consider this block as valid for noise measurement if the
219             // average term (sse - variance = N * avg^{2}, N = 16X16) of the
220             // temporal residual is small (avoid effects from lighting change).
221             if ((sse - variance) < thresh_sum_diff) {
222               unsigned int sse2;
223               const unsigned int spatial_variance = cpi->fn_ptr[bsize].vf(
224                   src_y, src_ystride, const_source, 0, &sse2);
225               // Avoid blocks with high brightness and high spatial variance.
226               if ((sse2 - spatial_variance) < thresh_sum_spatial &&
227                   spatial_variance < thresh_spatial_var) {
228                 avg_est += low_res ? variance >> 4
229                                    : variance / ((spatial_variance >> 9) + 1);
230                 num_samples++;
231               }
232             }
233           }
234         }
235         src_y += 8;
236         last_src_y += 8;
237         src_u += 4;
238         src_v += 4;
239       }
240       src_y += (src_ystride << 3) - (cm->mi_cols << 3);
241       last_src_y += (last_src_ystride << 3) - (cm->mi_cols << 3);
242       src_u += (src_uvstride << 2) - (cm->mi_cols << 2);
243       src_v += (src_uvstride << 2) - (cm->mi_cols << 2);
244     }
245     ne->last_w = cm->width;
246     ne->last_h = cm->height;
247     // Update noise estimate if we have at a minimum number of block samples,
248     // and avg_est > 0 (avg_est == 0 can happen if the application inputs
249     // duplicate frames).
250     if (num_samples > min_blocks_estimate && avg_est > 0) {
251       // Normalize.
252       avg_est = avg_est / num_samples;
253       // Update noise estimate.
254       ne->value = (int)((15 * ne->value + avg_est) >> 4);
255       ne->count++;
256       if (ne->count == ne->num_frames_estimate) {
257         // Reset counter and check noise level condition.
258         ne->num_frames_estimate = 30;
259         ne->count = 0;
260         ne->level = vp9_noise_estimate_extract_level(ne);
261 #if CONFIG_VP9_TEMPORAL_DENOISING
262         if (cpi->oxcf.noise_sensitivity > 0 && denoise_svc(cpi))
263           vp9_denoiser_set_noise_level(&cpi->denoiser, ne->level);
264 #endif
265       }
266     }
267   }
268 #if CONFIG_VP9_TEMPORAL_DENOISING
269   if (cpi->oxcf.noise_sensitivity > 0 && denoise_svc(cpi))
270     copy_frame(&cpi->denoiser.last_source, cpi->Source);
271 #endif
272 }
273