]> granicus.if.org Git - libvpx/blob - vp9/encoder/vp9_noise_estimate.c
Merge changes I92eb4312,Ibb2afe4e
[libvpx] / vp9 / encoder / vp9_noise_estimate.c
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 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 = 150;
36   } else if (width * height >= 640 * 360) {
37     ne->thresh = 100;
38   }
39   ne->num_frames_estimate = 15;
40 }
41
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
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
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
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 #endif
117   // Tune these thresholds for different resolutions.
118   if (cm->width > 640 && cm->width < 1920) {
119     thresh_consec_zeromv = 5;
120     thresh_sum_diff = 200;
121     thresh_sum_spatial = (120 * 120) << 8;
122     thresh_spatial_var = (48 * 48) << 8;
123   }
124   ne->enabled = enable_noise_estimation(cpi);
125   if (cpi->svc.number_spatial_layers > 1)
126     frame_counter = cpi->svc.current_superframe;
127   if (!ne->enabled || frame_counter % frame_period != 0 ||
128       last_source == NULL ||
129       (cpi->svc.number_spatial_layers == 1 &&
130        (ne->last_w != cm->width || ne->last_h != cm->height))) {
131 #if CONFIG_VP9_TEMPORAL_DENOISING
132     if (cpi->oxcf.noise_sensitivity > 0 && denoise_svc(cpi))
133       copy_frame(&cpi->denoiser.last_source, cpi->Source);
134 #endif
135     if (last_source != NULL) {
136       ne->last_w = cm->width;
137       ne->last_h = cm->height;
138     }
139     return;
140   } else if (cm->current_video_frame > 60 &&
141              cpi->rc.avg_frame_low_motion < (low_res ? 70 : 50)) {
142     // Force noise estimation to 0 and denoiser off if content has high motion.
143     ne->level = kLowLow;
144     ne->count = 0;
145     ne->num_frames_estimate = 10;
146 #if CONFIG_VP9_TEMPORAL_DENOISING
147     if (cpi->oxcf.noise_sensitivity > 0 && denoise_svc(cpi) &&
148         cpi->svc.current_superframe > 1) {
149       vp9_denoiser_set_noise_level(&cpi->denoiser, ne->level);
150       copy_frame(&cpi->denoiser.last_source, cpi->Source);
151     }
152 #endif
153     return;
154   } else {
155     int num_samples = 0;
156     uint64_t avg_est = 0;
157     int bsize = BLOCK_16X16;
158     static const unsigned char const_source[16] = { 0, 0, 0, 0, 0, 0, 0, 0,
159                                                     0, 0, 0, 0, 0, 0, 0, 0 };
160     // Loop over sub-sample of 16x16 blocks of frame, and for blocks that have
161     // been encoded as zero/small mv at least x consecutive frames, compute
162     // the variance to update estimate of noise in the source.
163     const uint8_t *src_y = cpi->Source->y_buffer;
164     const int src_ystride = cpi->Source->y_stride;
165     const uint8_t *last_src_y = last_source->y_buffer;
166     const int last_src_ystride = last_source->y_stride;
167     const uint8_t *src_u = cpi->Source->u_buffer;
168     const uint8_t *src_v = cpi->Source->v_buffer;
169     const int src_uvstride = cpi->Source->uv_stride;
170     int mi_row, mi_col;
171     int num_low_motion = 0;
172     int frame_low_motion = 1;
173     for (mi_row = 0; mi_row < cm->mi_rows; mi_row++) {
174       for (mi_col = 0; mi_col < cm->mi_cols; mi_col++) {
175         int bl_index = mi_row * cm->mi_cols + mi_col;
176         if (cpi->consec_zero_mv[bl_index] > thresh_consec_zeromv)
177           num_low_motion++;
178       }
179     }
180     if (num_low_motion < ((3 * cm->mi_rows * cm->mi_cols) >> 3))
181       frame_low_motion = 0;
182     for (mi_row = 0; mi_row < cm->mi_rows; mi_row++) {
183       for (mi_col = 0; mi_col < cm->mi_cols; mi_col++) {
184         // 16x16 blocks, 1/4 sample of frame.
185         if (mi_row % 4 == 0 && mi_col % 4 == 0 && mi_row < cm->mi_rows - 1 &&
186             mi_col < cm->mi_cols - 1) {
187           int bl_index = mi_row * cm->mi_cols + mi_col;
188           int bl_index1 = bl_index + 1;
189           int bl_index2 = bl_index + cm->mi_cols;
190           int bl_index3 = bl_index2 + 1;
191           // Only consider blocks that are likely steady background. i.e, have
192           // been encoded as zero/low motion x (= thresh_consec_zeromv) frames
193           // in a row. consec_zero_mv[] defined for 8x8 blocks, so consider all
194           // 4 sub-blocks for 16x16 block. Also, avoid skin blocks.
195           int consec_zeromv =
196               VPXMIN(cpi->consec_zero_mv[bl_index],
197                      VPXMIN(cpi->consec_zero_mv[bl_index1],
198                             VPXMIN(cpi->consec_zero_mv[bl_index2],
199                                    cpi->consec_zero_mv[bl_index3])));
200           int is_skin = 0;
201           if (cpi->use_skin_detection) {
202             is_skin =
203                 vp9_compute_skin_block(src_y, src_u, src_v, src_ystride,
204                                        src_uvstride, bsize, consec_zeromv, 0);
205           }
206           if (frame_low_motion &&
207               cpi->consec_zero_mv[bl_index] > thresh_consec_zeromv &&
208               cpi->consec_zero_mv[bl_index1] > thresh_consec_zeromv &&
209               cpi->consec_zero_mv[bl_index2] > thresh_consec_zeromv &&
210               cpi->consec_zero_mv[bl_index3] > thresh_consec_zeromv &&
211               !is_skin) {
212             // Compute variance.
213             unsigned int sse;
214             unsigned int variance = cpi->fn_ptr[bsize].vf(
215                 src_y, src_ystride, last_src_y, last_src_ystride, &sse);
216             // Only consider this block as valid for noise measurement if the
217             // average term (sse - variance = N * avg^{2}, N = 16X16) of the
218             // temporal residual is small (avoid effects from lighting change).
219             if ((sse - variance) < thresh_sum_diff) {
220               unsigned int sse2;
221               const unsigned int spatial_variance = cpi->fn_ptr[bsize].vf(
222                   src_y, src_ystride, const_source, 0, &sse2);
223               // Avoid blocks with high brightness and high spatial variance.
224               if ((sse2 - spatial_variance) < thresh_sum_spatial &&
225                   spatial_variance < thresh_spatial_var) {
226                 avg_est += low_res ? variance >> 4
227                                    : variance / ((spatial_variance >> 9) + 1);
228                 num_samples++;
229               }
230             }
231           }
232         }
233         src_y += 8;
234         last_src_y += 8;
235         src_u += 4;
236         src_v += 4;
237       }
238       src_y += (src_ystride << 3) - (cm->mi_cols << 3);
239       last_src_y += (last_src_ystride << 3) - (cm->mi_cols << 3);
240       src_u += (src_uvstride << 2) - (cm->mi_cols << 2);
241       src_v += (src_uvstride << 2) - (cm->mi_cols << 2);
242     }
243     ne->last_w = cm->width;
244     ne->last_h = cm->height;
245     // Update noise estimate if we have at a minimum number of block samples,
246     // and avg_est > 0 (avg_est == 0 can happen if the application inputs
247     // duplicate frames).
248     if (num_samples > min_blocks_estimate && avg_est > 0) {
249       // Normalize.
250       avg_est = avg_est / num_samples;
251       // Update noise estimate.
252       ne->value = (int)((15 * ne->value + avg_est) >> 4);
253       ne->count++;
254       if (ne->count == ne->num_frames_estimate) {
255         // Reset counter and check noise level condition.
256         ne->num_frames_estimate = 30;
257         ne->count = 0;
258         ne->level = vp9_noise_estimate_extract_level(ne);
259 #if CONFIG_VP9_TEMPORAL_DENOISING
260         if (cpi->oxcf.noise_sensitivity > 0 && denoise_svc(cpi))
261           vp9_denoiser_set_noise_level(&cpi->denoiser, ne->level);
262 #endif
263       }
264     }
265   }
266 #if CONFIG_VP9_TEMPORAL_DENOISING
267   if (cpi->oxcf.noise_sensitivity > 0 && denoise_svc(cpi))
268     copy_frame(&cpi->denoiser.last_source, cpi->Source);
269 #endif
270 }