xref: /aosp_15_r20/external/libaom/av1/encoder/av1_noise_estimate.c (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2020, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker  *
4*77c1e3ccSAndroid Build Coastguard Worker  * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker  * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker  * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker  */
11*77c1e3ccSAndroid Build Coastguard Worker 
12*77c1e3ccSAndroid Build Coastguard Worker #include <assert.h>
13*77c1e3ccSAndroid Build Coastguard Worker #include <limits.h>
14*77c1e3ccSAndroid Build Coastguard Worker #include <math.h>
15*77c1e3ccSAndroid Build Coastguard Worker 
16*77c1e3ccSAndroid Build Coastguard Worker #include "config/aom_dsp_rtcd.h"
17*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/aom_dsp_common.h"
18*77c1e3ccSAndroid Build Coastguard Worker #include "aom_scale/yv12config.h"
19*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aom_integer.h"
20*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/context_tree.h"
21*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/av1_noise_estimate.h"
22*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/encoder.h"
23*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_TEMPORAL_DENOISING
24*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/av1_temporal_denoiser.h"
25*77c1e3ccSAndroid Build Coastguard Worker #endif
26*77c1e3ccSAndroid Build Coastguard Worker 
27*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_TEMPORAL_DENOISING
28*77c1e3ccSAndroid Build Coastguard Worker // For SVC: only do noise estimation on top spatial layer.
noise_est_svc(const struct AV1_COMP * const cpi)29*77c1e3ccSAndroid Build Coastguard Worker static inline int noise_est_svc(const struct AV1_COMP *const cpi) {
30*77c1e3ccSAndroid Build Coastguard Worker   return (!cpi->ppi->use_svc ||
31*77c1e3ccSAndroid Build Coastguard Worker           (cpi->ppi->use_svc &&
32*77c1e3ccSAndroid Build Coastguard Worker            cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1));
33*77c1e3ccSAndroid Build Coastguard Worker }
34*77c1e3ccSAndroid Build Coastguard Worker #endif
35*77c1e3ccSAndroid Build Coastguard Worker 
av1_noise_estimate_init(NOISE_ESTIMATE * const ne,int width,int height)36*77c1e3ccSAndroid Build Coastguard Worker void av1_noise_estimate_init(NOISE_ESTIMATE *const ne, int width, int height) {
37*77c1e3ccSAndroid Build Coastguard Worker   const int64_t area = (int64_t)width * height;
38*77c1e3ccSAndroid Build Coastguard Worker   ne->enabled = 0;
39*77c1e3ccSAndroid Build Coastguard Worker   ne->level = (area < 1280 * 720) ? kLowLow : kLow;
40*77c1e3ccSAndroid Build Coastguard Worker   ne->value = 0;
41*77c1e3ccSAndroid Build Coastguard Worker   ne->count = 0;
42*77c1e3ccSAndroid Build Coastguard Worker   ne->thresh = 90;
43*77c1e3ccSAndroid Build Coastguard Worker   ne->last_w = 0;
44*77c1e3ccSAndroid Build Coastguard Worker   ne->last_h = 0;
45*77c1e3ccSAndroid Build Coastguard Worker   if (area >= 1920 * 1080) {
46*77c1e3ccSAndroid Build Coastguard Worker     ne->thresh = 200;
47*77c1e3ccSAndroid Build Coastguard Worker   } else if (area >= 1280 * 720) {
48*77c1e3ccSAndroid Build Coastguard Worker     ne->thresh = 140;
49*77c1e3ccSAndroid Build Coastguard Worker   } else if (area >= 640 * 360) {
50*77c1e3ccSAndroid Build Coastguard Worker     ne->thresh = 115;
51*77c1e3ccSAndroid Build Coastguard Worker   }
52*77c1e3ccSAndroid Build Coastguard Worker   ne->num_frames_estimate = 15;
53*77c1e3ccSAndroid Build Coastguard Worker   ne->adapt_thresh = (3 * ne->thresh) >> 1;
54*77c1e3ccSAndroid Build Coastguard Worker }
55*77c1e3ccSAndroid Build Coastguard Worker 
enable_noise_estimation(AV1_COMP * const cpi)56*77c1e3ccSAndroid Build Coastguard Worker static int enable_noise_estimation(AV1_COMP *const cpi) {
57*77c1e3ccSAndroid Build Coastguard Worker   const int resize_pending = is_frame_resize_pending(cpi);
58*77c1e3ccSAndroid Build Coastguard Worker 
59*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
60*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->common.seq_params->use_highbitdepth) return 0;
61*77c1e3ccSAndroid Build Coastguard Worker #endif
62*77c1e3ccSAndroid Build Coastguard Worker // Enable noise estimation if denoising is on.
63*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_TEMPORAL_DENOISING
64*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->oxcf.noise_sensitivity > 0 && noise_est_svc(cpi) &&
65*77c1e3ccSAndroid Build Coastguard Worker       cpi->common.width >= 320 && cpi->common.height >= 180)
66*77c1e3ccSAndroid Build Coastguard Worker     return 1;
67*77c1e3ccSAndroid Build Coastguard Worker #endif
68*77c1e3ccSAndroid Build Coastguard Worker   // Only allow noise estimate under certain encoding mode.
69*77c1e3ccSAndroid Build Coastguard Worker   // Enabled for 1 pass CBR, speed >=5, and if resolution is same as original.
70*77c1e3ccSAndroid Build Coastguard Worker   // Not enabled for SVC mode and screen_content_mode.
71*77c1e3ccSAndroid Build Coastguard Worker   // Not enabled for low resolutions.
72*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->oxcf.pass == AOM_RC_ONE_PASS && cpi->oxcf.rc_cfg.mode == AOM_CBR &&
73*77c1e3ccSAndroid Build Coastguard Worker       cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ && cpi->oxcf.speed >= 5 &&
74*77c1e3ccSAndroid Build Coastguard Worker       resize_pending == 0 && !cpi->ppi->use_svc &&
75*77c1e3ccSAndroid Build Coastguard Worker       cpi->oxcf.tune_cfg.content != AOM_CONTENT_SCREEN &&
76*77c1e3ccSAndroid Build Coastguard Worker       cpi->common.width * cpi->common.height >= 640 * 360)
77*77c1e3ccSAndroid Build Coastguard Worker     return 1;
78*77c1e3ccSAndroid Build Coastguard Worker   else
79*77c1e3ccSAndroid Build Coastguard Worker     return 0;
80*77c1e3ccSAndroid Build Coastguard Worker }
81*77c1e3ccSAndroid Build Coastguard Worker 
82*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_TEMPORAL_DENOISING
copy_frame(YV12_BUFFER_CONFIG * const dest,const YV12_BUFFER_CONFIG * const src)83*77c1e3ccSAndroid Build Coastguard Worker static void copy_frame(YV12_BUFFER_CONFIG *const dest,
84*77c1e3ccSAndroid Build Coastguard Worker                        const YV12_BUFFER_CONFIG *const src) {
85*77c1e3ccSAndroid Build Coastguard Worker   const uint8_t *srcbuf = src->y_buffer;
86*77c1e3ccSAndroid Build Coastguard Worker   uint8_t *destbuf = dest->y_buffer;
87*77c1e3ccSAndroid Build Coastguard Worker 
88*77c1e3ccSAndroid Build Coastguard Worker   assert(dest->y_width == src->y_width);
89*77c1e3ccSAndroid Build Coastguard Worker   assert(dest->y_height == src->y_height);
90*77c1e3ccSAndroid Build Coastguard Worker 
91*77c1e3ccSAndroid Build Coastguard Worker   for (int r = 0; r < dest->y_height; ++r) {
92*77c1e3ccSAndroid Build Coastguard Worker     memcpy(destbuf, srcbuf, dest->y_width);
93*77c1e3ccSAndroid Build Coastguard Worker     destbuf += dest->y_stride;
94*77c1e3ccSAndroid Build Coastguard Worker     srcbuf += src->y_stride;
95*77c1e3ccSAndroid Build Coastguard Worker   }
96*77c1e3ccSAndroid Build Coastguard Worker }
97*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_AV1_TEMPORAL_DENOISING
98*77c1e3ccSAndroid Build Coastguard Worker 
av1_noise_estimate_extract_level(NOISE_ESTIMATE * const ne)99*77c1e3ccSAndroid Build Coastguard Worker NOISE_LEVEL av1_noise_estimate_extract_level(NOISE_ESTIMATE *const ne) {
100*77c1e3ccSAndroid Build Coastguard Worker   int noise_level = kLowLow;
101*77c1e3ccSAndroid Build Coastguard Worker   if (ne->value > (ne->thresh << 1)) {
102*77c1e3ccSAndroid Build Coastguard Worker     noise_level = kHigh;
103*77c1e3ccSAndroid Build Coastguard Worker   } else {
104*77c1e3ccSAndroid Build Coastguard Worker     if (ne->value > ne->thresh)
105*77c1e3ccSAndroid Build Coastguard Worker       noise_level = kMedium;
106*77c1e3ccSAndroid Build Coastguard Worker     else if (ne->value > (ne->thresh >> 1))
107*77c1e3ccSAndroid Build Coastguard Worker       noise_level = kLow;
108*77c1e3ccSAndroid Build Coastguard Worker     else
109*77c1e3ccSAndroid Build Coastguard Worker       noise_level = kLowLow;
110*77c1e3ccSAndroid Build Coastguard Worker   }
111*77c1e3ccSAndroid Build Coastguard Worker   return noise_level;
112*77c1e3ccSAndroid Build Coastguard Worker }
113*77c1e3ccSAndroid Build Coastguard Worker 
av1_update_noise_estimate(AV1_COMP * const cpi)114*77c1e3ccSAndroid Build Coastguard Worker void av1_update_noise_estimate(AV1_COMP *const cpi) {
115*77c1e3ccSAndroid Build Coastguard Worker   const AV1_COMMON *const cm = &cpi->common;
116*77c1e3ccSAndroid Build Coastguard Worker   const CommonModeInfoParams *const mi_params = &cm->mi_params;
117*77c1e3ccSAndroid Build Coastguard Worker 
118*77c1e3ccSAndroid Build Coastguard Worker   NOISE_ESTIMATE *const ne = &cpi->noise_estimate;
119*77c1e3ccSAndroid Build Coastguard Worker   const int low_res = (cm->width <= 352 && cm->height <= 288);
120*77c1e3ccSAndroid Build Coastguard Worker   // Estimate of noise level every frame_period frames.
121*77c1e3ccSAndroid Build Coastguard Worker   int frame_period = 8;
122*77c1e3ccSAndroid Build Coastguard Worker   int thresh_consec_zeromv = 2;
123*77c1e3ccSAndroid Build Coastguard Worker   int frame_counter = cm->current_frame.frame_number;
124*77c1e3ccSAndroid Build Coastguard Worker   // Estimate is between current source and last source.
125*77c1e3ccSAndroid Build Coastguard Worker   YV12_BUFFER_CONFIG *last_source = cpi->last_source;
126*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_TEMPORAL_DENOISING
127*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->oxcf.noise_sensitivity > 0 && noise_est_svc(cpi)) {
128*77c1e3ccSAndroid Build Coastguard Worker     last_source = &cpi->denoiser.last_source;
129*77c1e3ccSAndroid Build Coastguard Worker     // Tune these thresholds for different resolutions when denoising is
130*77c1e3ccSAndroid Build Coastguard Worker     // enabled.
131*77c1e3ccSAndroid Build Coastguard Worker     if (cm->width > 640 && cm->width <= 1920) {
132*77c1e3ccSAndroid Build Coastguard Worker       thresh_consec_zeromv = 2;
133*77c1e3ccSAndroid Build Coastguard Worker     }
134*77c1e3ccSAndroid Build Coastguard Worker   }
135*77c1e3ccSAndroid Build Coastguard Worker #endif
136*77c1e3ccSAndroid Build Coastguard Worker   ne->enabled = enable_noise_estimation(cpi);
137*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->svc.number_spatial_layers > 1)
138*77c1e3ccSAndroid Build Coastguard Worker     frame_counter = cpi->svc.current_superframe;
139*77c1e3ccSAndroid Build Coastguard Worker   if (!ne->enabled || frame_counter % frame_period != 0 ||
140*77c1e3ccSAndroid Build Coastguard Worker       last_source == NULL ||
141*77c1e3ccSAndroid Build Coastguard Worker       (cpi->svc.number_spatial_layers == 1 &&
142*77c1e3ccSAndroid Build Coastguard Worker        (ne->last_w != cm->width || ne->last_h != cm->height))) {
143*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_TEMPORAL_DENOISING
144*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->oxcf.noise_sensitivity > 0 && noise_est_svc(cpi))
145*77c1e3ccSAndroid Build Coastguard Worker       copy_frame(&cpi->denoiser.last_source, cpi->source);
146*77c1e3ccSAndroid Build Coastguard Worker #endif
147*77c1e3ccSAndroid Build Coastguard Worker     if (last_source != NULL) {
148*77c1e3ccSAndroid Build Coastguard Worker       ne->last_w = cm->width;
149*77c1e3ccSAndroid Build Coastguard Worker       ne->last_h = cm->height;
150*77c1e3ccSAndroid Build Coastguard Worker     }
151*77c1e3ccSAndroid Build Coastguard Worker     return;
152*77c1e3ccSAndroid Build Coastguard Worker   } else if (frame_counter > 60 && cpi->svc.num_encoded_top_layer > 1 &&
153*77c1e3ccSAndroid Build Coastguard Worker              cpi->rc.frames_since_key > cpi->svc.number_spatial_layers &&
154*77c1e3ccSAndroid Build Coastguard Worker              cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1 &&
155*77c1e3ccSAndroid Build Coastguard Worker              cpi->rc.avg_frame_low_motion < (low_res ? 60 : 40)) {
156*77c1e3ccSAndroid Build Coastguard Worker     // Force noise estimation to 0 and denoiser off if content has high motion.
157*77c1e3ccSAndroid Build Coastguard Worker     ne->level = kLowLow;
158*77c1e3ccSAndroid Build Coastguard Worker     ne->count = 0;
159*77c1e3ccSAndroid Build Coastguard Worker     ne->num_frames_estimate = 10;
160*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_TEMPORAL_DENOISING
161*77c1e3ccSAndroid Build Coastguard Worker     if (cpi->oxcf.noise_sensitivity > 0 && noise_est_svc(cpi) &&
162*77c1e3ccSAndroid Build Coastguard Worker         cpi->svc.current_superframe > 1) {
163*77c1e3ccSAndroid Build Coastguard Worker       av1_denoiser_set_noise_level(cpi, ne->level);
164*77c1e3ccSAndroid Build Coastguard Worker       copy_frame(&cpi->denoiser.last_source, cpi->source);
165*77c1e3ccSAndroid Build Coastguard Worker     }
166*77c1e3ccSAndroid Build Coastguard Worker #endif
167*77c1e3ccSAndroid Build Coastguard Worker     return;
168*77c1e3ccSAndroid Build Coastguard Worker   } else {
169*77c1e3ccSAndroid Build Coastguard Worker     unsigned int bin_size = 100;
170*77c1e3ccSAndroid Build Coastguard Worker     unsigned int hist[MAX_VAR_HIST_BINS] = { 0 };
171*77c1e3ccSAndroid Build Coastguard Worker     unsigned int hist_avg[MAX_VAR_HIST_BINS];
172*77c1e3ccSAndroid Build Coastguard Worker     unsigned int max_bin = 0;
173*77c1e3ccSAndroid Build Coastguard Worker     unsigned int max_bin_count = 0;
174*77c1e3ccSAndroid Build Coastguard Worker     unsigned int bin_cnt;
175*77c1e3ccSAndroid Build Coastguard Worker     BLOCK_SIZE bsize = BLOCK_16X16;
176*77c1e3ccSAndroid Build Coastguard Worker     // Loop over sub-sample of 16x16 blocks of frame, and for blocks that have
177*77c1e3ccSAndroid Build Coastguard Worker     // been encoded as zero/small mv at least x consecutive frames, compute
178*77c1e3ccSAndroid Build Coastguard Worker     // the variance to update estimate of noise in the source.
179*77c1e3ccSAndroid Build Coastguard Worker     const uint8_t *src_y = cpi->source->y_buffer;
180*77c1e3ccSAndroid Build Coastguard Worker     const int src_ystride = cpi->source->y_stride;
181*77c1e3ccSAndroid Build Coastguard Worker     const uint8_t *last_src_y = last_source->y_buffer;
182*77c1e3ccSAndroid Build Coastguard Worker     const int last_src_ystride = last_source->y_stride;
183*77c1e3ccSAndroid Build Coastguard Worker     int mi_row, mi_col;
184*77c1e3ccSAndroid Build Coastguard Worker     int num_low_motion = 0;
185*77c1e3ccSAndroid Build Coastguard Worker     int frame_low_motion = 1;
186*77c1e3ccSAndroid Build Coastguard Worker     for (mi_row = 0; mi_row < mi_params->mi_rows; mi_row += 2) {
187*77c1e3ccSAndroid Build Coastguard Worker       for (mi_col = 0; mi_col < mi_params->mi_cols; mi_col += 2) {
188*77c1e3ccSAndroid Build Coastguard Worker         int bl_index =
189*77c1e3ccSAndroid Build Coastguard Worker             (mi_row >> 1) * (mi_params->mi_cols >> 1) + (mi_col >> 1);
190*77c1e3ccSAndroid Build Coastguard Worker         if (cpi->consec_zero_mv[bl_index] > thresh_consec_zeromv)
191*77c1e3ccSAndroid Build Coastguard Worker           num_low_motion++;
192*77c1e3ccSAndroid Build Coastguard Worker       }
193*77c1e3ccSAndroid Build Coastguard Worker     }
194*77c1e3ccSAndroid Build Coastguard Worker     if (num_low_motion <
195*77c1e3ccSAndroid Build Coastguard Worker         (((3 * (mi_params->mi_rows * mi_params->mi_cols) >> 2)) >> 3))
196*77c1e3ccSAndroid Build Coastguard Worker       frame_low_motion = 0;
197*77c1e3ccSAndroid Build Coastguard Worker     for (mi_row = 0; mi_row < mi_params->mi_rows; mi_row++) {
198*77c1e3ccSAndroid Build Coastguard Worker       for (mi_col = 0; mi_col < mi_params->mi_cols; mi_col++) {
199*77c1e3ccSAndroid Build Coastguard Worker         // 16x16 blocks, 1/4 sample of frame.
200*77c1e3ccSAndroid Build Coastguard Worker         if (mi_row % 8 == 0 && mi_col % 8 == 0 &&
201*77c1e3ccSAndroid Build Coastguard Worker             mi_row < mi_params->mi_rows - 3 &&
202*77c1e3ccSAndroid Build Coastguard Worker             mi_col < mi_params->mi_cols - 3) {
203*77c1e3ccSAndroid Build Coastguard Worker           int bl_index =
204*77c1e3ccSAndroid Build Coastguard Worker               (mi_row >> 1) * (mi_params->mi_cols >> 1) + (mi_col >> 1);
205*77c1e3ccSAndroid Build Coastguard Worker           int bl_index1 = bl_index + 1;
206*77c1e3ccSAndroid Build Coastguard Worker           int bl_index2 = bl_index + (mi_params->mi_cols >> 1);
207*77c1e3ccSAndroid Build Coastguard Worker           int bl_index3 = bl_index2 + 1;
208*77c1e3ccSAndroid Build Coastguard Worker           int consec_zeromv =
209*77c1e3ccSAndroid Build Coastguard Worker               AOMMIN(cpi->consec_zero_mv[bl_index],
210*77c1e3ccSAndroid Build Coastguard Worker                      AOMMIN(cpi->consec_zero_mv[bl_index1],
211*77c1e3ccSAndroid Build Coastguard Worker                             AOMMIN(cpi->consec_zero_mv[bl_index2],
212*77c1e3ccSAndroid Build Coastguard Worker                                    cpi->consec_zero_mv[bl_index3])));
213*77c1e3ccSAndroid Build Coastguard Worker           // Only consider blocks that are likely steady background. i.e, have
214*77c1e3ccSAndroid Build Coastguard Worker           // been encoded as zero/low motion x (= thresh_consec_zeromv) frames
215*77c1e3ccSAndroid Build Coastguard Worker           // in a row. consec_zero_mv[] defined for 8x8 blocks, so consider all
216*77c1e3ccSAndroid Build Coastguard Worker           // 4 sub-blocks for 16x16 block. And exclude this frame if
217*77c1e3ccSAndroid Build Coastguard Worker           // high_source_sad is true (i.e., scene/content change).
218*77c1e3ccSAndroid Build Coastguard Worker           if (frame_low_motion && consec_zeromv > thresh_consec_zeromv &&
219*77c1e3ccSAndroid Build Coastguard Worker               !cpi->rc.high_source_sad) {
220*77c1e3ccSAndroid Build Coastguard Worker             unsigned int sse;
221*77c1e3ccSAndroid Build Coastguard Worker             // Compute variance between co-located blocks from current and
222*77c1e3ccSAndroid Build Coastguard Worker             // last input frames.
223*77c1e3ccSAndroid Build Coastguard Worker             unsigned int variance = cpi->ppi->fn_ptr[bsize].vf(
224*77c1e3ccSAndroid Build Coastguard Worker                 src_y, src_ystride, last_src_y, last_src_ystride, &sse);
225*77c1e3ccSAndroid Build Coastguard Worker             unsigned int hist_index = variance / bin_size;
226*77c1e3ccSAndroid Build Coastguard Worker             if (hist_index < MAX_VAR_HIST_BINS)
227*77c1e3ccSAndroid Build Coastguard Worker               hist[hist_index]++;
228*77c1e3ccSAndroid Build Coastguard Worker             else if (hist_index < 3 * (MAX_VAR_HIST_BINS >> 1))
229*77c1e3ccSAndroid Build Coastguard Worker               hist[MAX_VAR_HIST_BINS - 1]++;  // Account for the tail
230*77c1e3ccSAndroid Build Coastguard Worker           }
231*77c1e3ccSAndroid Build Coastguard Worker         }
232*77c1e3ccSAndroid Build Coastguard Worker         src_y += 4;
233*77c1e3ccSAndroid Build Coastguard Worker         last_src_y += 4;
234*77c1e3ccSAndroid Build Coastguard Worker       }
235*77c1e3ccSAndroid Build Coastguard Worker       src_y += (src_ystride << 2) - (mi_params->mi_cols << 2);
236*77c1e3ccSAndroid Build Coastguard Worker       last_src_y += (last_src_ystride << 2) - (mi_params->mi_cols << 2);
237*77c1e3ccSAndroid Build Coastguard Worker     }
238*77c1e3ccSAndroid Build Coastguard Worker     ne->last_w = cm->width;
239*77c1e3ccSAndroid Build Coastguard Worker     ne->last_h = cm->height;
240*77c1e3ccSAndroid Build Coastguard Worker     // Adjust histogram to account for effect that histogram flattens
241*77c1e3ccSAndroid Build Coastguard Worker     // and shifts to zero as scene darkens.
242*77c1e3ccSAndroid Build Coastguard Worker     if (hist[0] > 10 && (hist[MAX_VAR_HIST_BINS - 1] > hist[0] >> 2)) {
243*77c1e3ccSAndroid Build Coastguard Worker       hist[0] = 0;
244*77c1e3ccSAndroid Build Coastguard Worker       hist[1] >>= 2;
245*77c1e3ccSAndroid Build Coastguard Worker       hist[2] >>= 2;
246*77c1e3ccSAndroid Build Coastguard Worker       hist[3] >>= 2;
247*77c1e3ccSAndroid Build Coastguard Worker       hist[4] >>= 1;
248*77c1e3ccSAndroid Build Coastguard Worker       hist[5] >>= 1;
249*77c1e3ccSAndroid Build Coastguard Worker       hist[6] = 3 * hist[6] >> 1;
250*77c1e3ccSAndroid Build Coastguard Worker       hist[MAX_VAR_HIST_BINS - 1] >>= 1;
251*77c1e3ccSAndroid Build Coastguard Worker     }
252*77c1e3ccSAndroid Build Coastguard Worker 
253*77c1e3ccSAndroid Build Coastguard Worker     // Average hist[] and find largest bin
254*77c1e3ccSAndroid Build Coastguard Worker     for (bin_cnt = 0; bin_cnt < MAX_VAR_HIST_BINS; bin_cnt++) {
255*77c1e3ccSAndroid Build Coastguard Worker       if (bin_cnt == 0)
256*77c1e3ccSAndroid Build Coastguard Worker         hist_avg[bin_cnt] = (hist[0] + hist[1] + hist[2]) / 3;
257*77c1e3ccSAndroid Build Coastguard Worker       else if (bin_cnt == MAX_VAR_HIST_BINS - 1)
258*77c1e3ccSAndroid Build Coastguard Worker         hist_avg[bin_cnt] = hist[MAX_VAR_HIST_BINS - 1] >> 2;
259*77c1e3ccSAndroid Build Coastguard Worker       else if (bin_cnt == MAX_VAR_HIST_BINS - 2)
260*77c1e3ccSAndroid Build Coastguard Worker         hist_avg[bin_cnt] = (hist[bin_cnt - 1] + 2 * hist[bin_cnt] +
261*77c1e3ccSAndroid Build Coastguard Worker                              (hist[bin_cnt + 1] >> 1) + 2) >>
262*77c1e3ccSAndroid Build Coastguard Worker                             2;
263*77c1e3ccSAndroid Build Coastguard Worker       else
264*77c1e3ccSAndroid Build Coastguard Worker         hist_avg[bin_cnt] =
265*77c1e3ccSAndroid Build Coastguard Worker             (hist[bin_cnt - 1] + 2 * hist[bin_cnt] + hist[bin_cnt + 1] + 2) >>
266*77c1e3ccSAndroid Build Coastguard Worker             2;
267*77c1e3ccSAndroid Build Coastguard Worker 
268*77c1e3ccSAndroid Build Coastguard Worker       if (hist_avg[bin_cnt] > max_bin_count) {
269*77c1e3ccSAndroid Build Coastguard Worker         max_bin_count = hist_avg[bin_cnt];
270*77c1e3ccSAndroid Build Coastguard Worker         max_bin = bin_cnt;
271*77c1e3ccSAndroid Build Coastguard Worker       }
272*77c1e3ccSAndroid Build Coastguard Worker     }
273*77c1e3ccSAndroid Build Coastguard Worker     // Scale by 40 to work with existing thresholds
274*77c1e3ccSAndroid Build Coastguard Worker     ne->value = (int)((3 * ne->value + max_bin * 40) >> 2);
275*77c1e3ccSAndroid Build Coastguard Worker     // Quickly increase VNR strength when the noise level increases suddenly.
276*77c1e3ccSAndroid Build Coastguard Worker     if (ne->level < kMedium && ne->value > ne->adapt_thresh) {
277*77c1e3ccSAndroid Build Coastguard Worker       ne->count = ne->num_frames_estimate;
278*77c1e3ccSAndroid Build Coastguard Worker     } else {
279*77c1e3ccSAndroid Build Coastguard Worker       ne->count++;
280*77c1e3ccSAndroid Build Coastguard Worker     }
281*77c1e3ccSAndroid Build Coastguard Worker     if (ne->count == ne->num_frames_estimate) {
282*77c1e3ccSAndroid Build Coastguard Worker       // Reset counter and check noise level condition.
283*77c1e3ccSAndroid Build Coastguard Worker       ne->num_frames_estimate = 30;
284*77c1e3ccSAndroid Build Coastguard Worker       ne->count = 0;
285*77c1e3ccSAndroid Build Coastguard Worker       ne->level = av1_noise_estimate_extract_level(ne);
286*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_TEMPORAL_DENOISING
287*77c1e3ccSAndroid Build Coastguard Worker       if (cpi->oxcf.noise_sensitivity > 0 && noise_est_svc(cpi))
288*77c1e3ccSAndroid Build Coastguard Worker         av1_denoiser_set_noise_level(cpi, ne->level);
289*77c1e3ccSAndroid Build Coastguard Worker #endif
290*77c1e3ccSAndroid Build Coastguard Worker     }
291*77c1e3ccSAndroid Build Coastguard Worker   }
292*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_TEMPORAL_DENOISING
293*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->oxcf.noise_sensitivity > 0 && noise_est_svc(cpi))
294*77c1e3ccSAndroid Build Coastguard Worker     copy_frame(&cpi->denoiser.last_source, cpi->source);
295*77c1e3ccSAndroid Build Coastguard Worker #endif
296*77c1e3ccSAndroid Build Coastguard Worker }
297