2015-11-01 23:40:05 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015 The WebM project authors. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#include "./vpx_dsp_rtcd.h"
|
|
|
|
#include "vpx_dsp/vpx_dsp_common.h"
|
|
|
|
#include "vpx_scale/yv12config.h"
|
|
|
|
#include "vpx/vpx_integer.h"
|
|
|
|
#include "vp9/common/vp9_reconinter.h"
|
|
|
|
#include "vp9/encoder/vp9_context_tree.h"
|
|
|
|
#include "vp9/encoder/vp9_noise_estimate.h"
|
|
|
|
#include "vp9/encoder/vp9_encoder.h"
|
|
|
|
|
2016-07-27 05:43:23 +02:00
|
|
|
void vp9_noise_estimate_init(NOISE_ESTIMATE *const ne, int width, int height) {
|
2015-11-01 23:40:05 +01:00
|
|
|
ne->enabled = 0;
|
2015-12-15 17:44:40 +01:00
|
|
|
ne->level = kLowLow;
|
2015-11-01 23:40:05 +01:00
|
|
|
ne->value = 0;
|
|
|
|
ne->count = 0;
|
2017-05-01 19:49:54 +02:00
|
|
|
ne->thresh = 90;
|
2015-11-10 04:18:29 +01:00
|
|
|
ne->last_w = 0;
|
|
|
|
ne->last_h = 0;
|
2015-11-01 23:40:05 +01:00
|
|
|
if (width * height >= 1920 * 1080) {
|
2015-11-06 01:00:15 +01:00
|
|
|
ne->thresh = 200;
|
2015-11-01 23:40:05 +01:00
|
|
|
} else if (width * height >= 1280 * 720) {
|
2017-05-11 01:46:50 +02:00
|
|
|
ne->thresh = 140;
|
2017-05-01 19:49:54 +02:00
|
|
|
} else if (width * height >= 640 * 360) {
|
|
|
|
ne->thresh = 100;
|
2015-11-01 23:40:05 +01:00
|
|
|
}
|
2017-05-01 19:49:54 +02:00
|
|
|
ne->num_frames_estimate = 15;
|
2015-11-01 23:40:05 +01:00
|
|
|
}
|
|
|
|
|
2016-05-05 00:09:05 +02:00
|
|
|
static int enable_noise_estimation(VP9_COMP *const cpi) {
|
2017-01-26 23:09:57 +01:00
|
|
|
#if CONFIG_VP9_HIGHBITDEPTH
|
|
|
|
if (cpi->common.use_highbitdepth) return 0;
|
|
|
|
#endif
|
2017-05-01 19:49:54 +02:00
|
|
|
// Enable noise estimation if denoising is on.
|
2015-11-01 23:40:05 +01:00
|
|
|
#if CONFIG_VP9_TEMPORAL_DENOISING
|
2016-12-21 23:33:21 +01:00
|
|
|
if (cpi->oxcf.noise_sensitivity > 0 && denoise_svc(cpi) &&
|
2017-05-01 19:49:54 +02:00
|
|
|
cpi->common.width >= 320 && cpi->common.height >= 180)
|
2016-12-17 01:01:59 +01:00
|
|
|
return 1;
|
2015-11-01 23:40:05 +01:00
|
|
|
#endif
|
|
|
|
// Only allow noise estimate under certain encoding mode.
|
|
|
|
// Enabled for 1 pass CBR, speed >=5, and if resolution is same as original.
|
|
|
|
// Not enabled for SVC mode and screen_content_mode.
|
|
|
|
// Not enabled for low resolutions.
|
2016-07-27 05:43:23 +02:00
|
|
|
if (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == VPX_CBR &&
|
|
|
|
cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cpi->oxcf.speed >= 5 &&
|
|
|
|
cpi->resize_state == ORIG && cpi->resize_pending == 0 && !cpi->use_svc &&
|
|
|
|
cpi->oxcf.content != VP9E_CONTENT_SCREEN && cpi->common.width >= 640 &&
|
2017-01-04 01:01:05 +01:00
|
|
|
cpi->common.height >= 360)
|
2015-11-01 23:40:05 +01:00
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-01-27 05:09:36 +01:00
|
|
|
#if CONFIG_VP9_TEMPORAL_DENOISING
|
2016-07-27 05:43:23 +02:00
|
|
|
static void copy_frame(YV12_BUFFER_CONFIG *const dest,
|
|
|
|
const YV12_BUFFER_CONFIG *const src) {
|
2015-11-01 23:40:05 +01:00
|
|
|
int r;
|
|
|
|
const uint8_t *srcbuf = src->y_buffer;
|
|
|
|
uint8_t *destbuf = dest->y_buffer;
|
|
|
|
|
|
|
|
assert(dest->y_width == src->y_width);
|
|
|
|
assert(dest->y_height == src->y_height);
|
|
|
|
|
|
|
|
for (r = 0; r < dest->y_height; ++r) {
|
|
|
|
memcpy(destbuf, srcbuf, dest->y_width);
|
|
|
|
destbuf += dest->y_stride;
|
|
|
|
srcbuf += src->y_stride;
|
|
|
|
}
|
|
|
|
}
|
2016-01-27 05:09:36 +01:00
|
|
|
#endif // CONFIG_VP9_TEMPORAL_DENOISING
|
2015-11-01 23:40:05 +01:00
|
|
|
|
2015-12-03 22:08:37 +01:00
|
|
|
NOISE_LEVEL vp9_noise_estimate_extract_level(NOISE_ESTIMATE *const ne) {
|
2015-12-15 17:44:40 +01:00
|
|
|
int noise_level = kLowLow;
|
2015-12-03 22:08:37 +01:00
|
|
|
if (ne->value > (ne->thresh << 1)) {
|
|
|
|
noise_level = kHigh;
|
|
|
|
} else {
|
|
|
|
if (ne->value > ne->thresh)
|
|
|
|
noise_level = kMedium;
|
2016-04-04 21:30:50 +02:00
|
|
|
else if (ne->value > ((9 * ne->thresh) >> 4))
|
2015-12-03 22:08:37 +01:00
|
|
|
noise_level = kLow;
|
|
|
|
else
|
|
|
|
noise_level = kLowLow;
|
|
|
|
}
|
|
|
|
return noise_level;
|
|
|
|
}
|
|
|
|
|
2015-11-01 23:40:05 +01:00
|
|
|
void vp9_update_noise_estimate(VP9_COMP *const cpi) {
|
|
|
|
const VP9_COMMON *const cm = &cpi->common;
|
|
|
|
NOISE_ESTIMATE *const ne = &cpi->noise_estimate;
|
2017-05-01 19:49:54 +02:00
|
|
|
const int low_res = (cm->width <= 352 && cm->height <= 288);
|
2015-11-01 23:40:05 +01:00
|
|
|
// Estimate of noise level every frame_period frames.
|
2016-05-04 20:10:02 +02:00
|
|
|
int frame_period = 8;
|
2016-04-25 23:34:41 +02:00
|
|
|
int thresh_consec_zeromv = 6;
|
2015-11-13 03:36:17 +01:00
|
|
|
unsigned int thresh_sum_diff = 100;
|
2015-11-06 01:00:15 +01:00
|
|
|
unsigned int thresh_sum_spatial = (200 * 200) << 8;
|
|
|
|
unsigned int thresh_spatial_var = (32 * 32) << 8;
|
2015-11-01 23:40:05 +01:00
|
|
|
int min_blocks_estimate = cm->mi_rows * cm->mi_cols >> 7;
|
2016-12-21 23:33:21 +01:00
|
|
|
int frame_counter = cm->current_video_frame;
|
2015-11-01 23:40:05 +01:00
|
|
|
// Estimate is between current source and last source.
|
|
|
|
YV12_BUFFER_CONFIG *last_source = cpi->Last_Source;
|
|
|
|
#if CONFIG_VP9_TEMPORAL_DENOISING
|
2017-05-10 02:23:00 +02:00
|
|
|
if (cpi->oxcf.noise_sensitivity > 0 && denoise_svc(cpi)) {
|
2016-12-21 23:33:21 +01:00
|
|
|
last_source = &cpi->denoiser.last_source;
|
2017-05-10 02:23:00 +02:00
|
|
|
// Tune these thresholds for different resolutions when denoising is
|
|
|
|
// enabled.
|
|
|
|
if (cm->width > 640 && cm->width < 1920) {
|
2017-05-11 01:46:50 +02:00
|
|
|
thresh_consec_zeromv = 4;
|
2017-05-10 02:23:00 +02:00
|
|
|
thresh_sum_diff = 200;
|
|
|
|
thresh_sum_spatial = (120 * 120) << 8;
|
|
|
|
thresh_spatial_var = (48 * 48) << 8;
|
|
|
|
}
|
2017-05-05 02:34:19 +02:00
|
|
|
}
|
2017-05-10 02:23:00 +02:00
|
|
|
#endif
|
2015-11-01 23:40:05 +01:00
|
|
|
ne->enabled = enable_noise_estimation(cpi);
|
2016-12-21 23:33:21 +01:00
|
|
|
if (cpi->svc.number_spatial_layers > 1)
|
|
|
|
frame_counter = cpi->svc.current_superframe;
|
|
|
|
if (!ne->enabled || frame_counter % frame_period != 0 ||
|
|
|
|
last_source == NULL ||
|
|
|
|
(cpi->svc.number_spatial_layers == 1 &&
|
|
|
|
(ne->last_w != cm->width || ne->last_h != cm->height))) {
|
2015-11-01 23:40:05 +01:00
|
|
|
#if CONFIG_VP9_TEMPORAL_DENOISING
|
2016-12-21 23:33:21 +01:00
|
|
|
if (cpi->oxcf.noise_sensitivity > 0 && denoise_svc(cpi))
|
2016-07-27 05:43:23 +02:00
|
|
|
copy_frame(&cpi->denoiser.last_source, cpi->Source);
|
2015-11-01 23:40:05 +01:00
|
|
|
#endif
|
2015-11-10 04:18:29 +01:00
|
|
|
if (last_source != NULL) {
|
|
|
|
ne->last_w = cm->width;
|
|
|
|
ne->last_h = cm->height;
|
|
|
|
}
|
2015-11-01 23:40:05 +01:00
|
|
|
return;
|
2017-05-05 02:34:19 +02:00
|
|
|
} else if (cm->current_video_frame > 60 &&
|
|
|
|
cpi->rc.avg_frame_low_motion < (low_res ? 70 : 50)) {
|
2016-04-23 00:27:53 +02:00
|
|
|
// Force noise estimation to 0 and denoiser off if content has high motion.
|
|
|
|
ne->level = kLowLow;
|
2017-05-10 18:36:10 +02:00
|
|
|
ne->count = 0;
|
|
|
|
ne->num_frames_estimate = 10;
|
2016-04-23 00:27:53 +02:00
|
|
|
#if CONFIG_VP9_TEMPORAL_DENOISING
|
2017-03-16 00:51:34 +01:00
|
|
|
if (cpi->oxcf.noise_sensitivity > 0 && denoise_svc(cpi) &&
|
2017-04-07 01:35:01 +02:00
|
|
|
cpi->svc.current_superframe > 1) {
|
2016-04-23 00:27:53 +02:00
|
|
|
vp9_denoiser_set_noise_level(&cpi->denoiser, ne->level);
|
2017-04-07 01:35:01 +02:00
|
|
|
copy_frame(&cpi->denoiser.last_source, cpi->Source);
|
|
|
|
}
|
2016-04-23 00:27:53 +02:00
|
|
|
#endif
|
|
|
|
return;
|
2015-11-01 23:40:05 +01:00
|
|
|
} else {
|
|
|
|
int num_samples = 0;
|
|
|
|
uint64_t avg_est = 0;
|
|
|
|
int bsize = BLOCK_16X16;
|
2016-07-27 05:43:23 +02:00
|
|
|
static const unsigned char const_source[16] = { 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0 };
|
2015-11-01 23:40:05 +01:00
|
|
|
// Loop over sub-sample of 16x16 blocks of frame, and for blocks that have
|
|
|
|
// been encoded as zero/small mv at least x consecutive frames, compute
|
|
|
|
// the variance to update estimate of noise in the source.
|
|
|
|
const uint8_t *src_y = cpi->Source->y_buffer;
|
|
|
|
const int src_ystride = cpi->Source->y_stride;
|
|
|
|
const uint8_t *last_src_y = last_source->y_buffer;
|
|
|
|
const int last_src_ystride = last_source->y_stride;
|
|
|
|
const uint8_t *src_u = cpi->Source->u_buffer;
|
|
|
|
const uint8_t *src_v = cpi->Source->v_buffer;
|
|
|
|
const int src_uvstride = cpi->Source->uv_stride;
|
|
|
|
int mi_row, mi_col;
|
2015-11-13 03:36:17 +01:00
|
|
|
int num_low_motion = 0;
|
|
|
|
int frame_low_motion = 1;
|
2017-07-11 01:16:05 +02:00
|
|
|
if (cpi->use_skin_detection) vp9_compute_skin_map(cpi, BLOCK_16X16);
|
2015-11-13 03:36:17 +01:00
|
|
|
for (mi_row = 0; mi_row < cm->mi_rows; mi_row++) {
|
|
|
|
for (mi_col = 0; mi_col < cm->mi_cols; mi_col++) {
|
|
|
|
int bl_index = mi_row * cm->mi_cols + mi_col;
|
2016-04-21 23:00:26 +02:00
|
|
|
if (cpi->consec_zero_mv[bl_index] > thresh_consec_zeromv)
|
2015-11-13 03:36:17 +01:00
|
|
|
num_low_motion++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (num_low_motion < ((3 * cm->mi_rows * cm->mi_cols) >> 3))
|
|
|
|
frame_low_motion = 0;
|
2015-11-01 23:40:05 +01:00
|
|
|
for (mi_row = 0; mi_row < cm->mi_rows; mi_row++) {
|
|
|
|
for (mi_col = 0; mi_col < cm->mi_cols; mi_col++) {
|
|
|
|
// 16x16 blocks, 1/4 sample of frame.
|
2016-07-27 05:43:23 +02:00
|
|
|
if (mi_row % 4 == 0 && mi_col % 4 == 0 && mi_row < cm->mi_rows - 1 &&
|
2016-02-01 07:24:55 +01:00
|
|
|
mi_col < cm->mi_cols - 1) {
|
2015-11-01 23:40:05 +01:00
|
|
|
int bl_index = mi_row * cm->mi_cols + mi_col;
|
|
|
|
int bl_index1 = bl_index + 1;
|
|
|
|
int bl_index2 = bl_index + cm->mi_cols;
|
|
|
|
int bl_index3 = bl_index2 + 1;
|
2017-06-27 22:17:59 +02:00
|
|
|
int is_skin = 0;
|
|
|
|
if (cpi->use_skin_detection) is_skin = cpi->skin_map[bl_index];
|
2015-11-01 23:40:05 +01:00
|
|
|
// Only consider blocks that are likely steady background. i.e, have
|
|
|
|
// been encoded as zero/low motion x (= thresh_consec_zeromv) frames
|
|
|
|
// in a row. consec_zero_mv[] defined for 8x8 blocks, so consider all
|
|
|
|
// 4 sub-blocks for 16x16 block. Also, avoid skin blocks.
|
2015-11-13 03:36:17 +01:00
|
|
|
if (frame_low_motion &&
|
2016-04-21 23:00:26 +02:00
|
|
|
cpi->consec_zero_mv[bl_index] > thresh_consec_zeromv &&
|
|
|
|
cpi->consec_zero_mv[bl_index1] > thresh_consec_zeromv &&
|
|
|
|
cpi->consec_zero_mv[bl_index2] > thresh_consec_zeromv &&
|
|
|
|
cpi->consec_zero_mv[bl_index3] > thresh_consec_zeromv &&
|
2015-11-01 23:40:05 +01:00
|
|
|
!is_skin) {
|
|
|
|
// Compute variance.
|
|
|
|
unsigned int sse;
|
2016-07-27 05:43:23 +02:00
|
|
|
unsigned int variance = cpi->fn_ptr[bsize].vf(
|
|
|
|
src_y, src_ystride, last_src_y, last_src_ystride, &sse);
|
2015-11-01 23:40:05 +01:00
|
|
|
// Only consider this block as valid for noise measurement if the
|
|
|
|
// average term (sse - variance = N * avg^{2}, N = 16X16) of the
|
|
|
|
// temporal residual is small (avoid effects from lighting change).
|
|
|
|
if ((sse - variance) < thresh_sum_diff) {
|
|
|
|
unsigned int sse2;
|
2016-07-27 05:43:23 +02:00
|
|
|
const unsigned int spatial_variance = cpi->fn_ptr[bsize].vf(
|
|
|
|
src_y, src_ystride, const_source, 0, &sse2);
|
2015-11-06 01:00:15 +01:00
|
|
|
// Avoid blocks with high brightness and high spatial variance.
|
|
|
|
if ((sse2 - spatial_variance) < thresh_sum_spatial &&
|
|
|
|
spatial_variance < thresh_spatial_var) {
|
2017-05-01 19:49:54 +02:00
|
|
|
avg_est += low_res ? variance >> 4
|
|
|
|
: variance / ((spatial_variance >> 9) + 1);
|
2015-11-06 01:00:15 +01:00
|
|
|
num_samples++;
|
|
|
|
}
|
2015-11-01 23:40:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
src_y += 8;
|
|
|
|
last_src_y += 8;
|
|
|
|
src_u += 4;
|
|
|
|
src_v += 4;
|
|
|
|
}
|
|
|
|
src_y += (src_ystride << 3) - (cm->mi_cols << 3);
|
|
|
|
last_src_y += (last_src_ystride << 3) - (cm->mi_cols << 3);
|
|
|
|
src_u += (src_uvstride << 2) - (cm->mi_cols << 2);
|
|
|
|
src_v += (src_uvstride << 2) - (cm->mi_cols << 2);
|
|
|
|
}
|
2015-11-10 04:18:29 +01:00
|
|
|
ne->last_w = cm->width;
|
|
|
|
ne->last_h = cm->height;
|
2015-11-01 23:40:05 +01:00
|
|
|
// Update noise estimate if we have at a minimum number of block samples,
|
|
|
|
// and avg_est > 0 (avg_est == 0 can happen if the application inputs
|
|
|
|
// duplicate frames).
|
|
|
|
if (num_samples > min_blocks_estimate && avg_est > 0) {
|
|
|
|
// Normalize.
|
2015-11-06 01:00:15 +01:00
|
|
|
avg_est = avg_est / num_samples;
|
2015-11-01 23:40:05 +01:00
|
|
|
// Update noise estimate.
|
2015-11-13 03:36:17 +01:00
|
|
|
ne->value = (int)((15 * ne->value + avg_est) >> 4);
|
2015-11-01 23:40:05 +01:00
|
|
|
ne->count++;
|
2015-11-13 17:09:17 +01:00
|
|
|
if (ne->count == ne->num_frames_estimate) {
|
2015-11-01 23:40:05 +01:00
|
|
|
// Reset counter and check noise level condition.
|
2015-11-13 17:09:17 +01:00
|
|
|
ne->num_frames_estimate = 30;
|
2015-11-01 23:40:05 +01:00
|
|
|
ne->count = 0;
|
2015-12-03 22:08:37 +01:00
|
|
|
ne->level = vp9_noise_estimate_extract_level(ne);
|
2015-11-30 17:51:25 +01:00
|
|
|
#if CONFIG_VP9_TEMPORAL_DENOISING
|
2016-12-21 23:33:21 +01:00
|
|
|
if (cpi->oxcf.noise_sensitivity > 0 && denoise_svc(cpi))
|
2015-11-30 17:51:25 +01:00
|
|
|
vp9_denoiser_set_noise_level(&cpi->denoiser, ne->level);
|
|
|
|
#endif
|
2015-11-01 23:40:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#if CONFIG_VP9_TEMPORAL_DENOISING
|
2016-12-21 23:33:21 +01:00
|
|
|
if (cpi->oxcf.noise_sensitivity > 0 && denoise_svc(cpi))
|
2015-11-01 23:40:05 +01:00
|
|
|
copy_frame(&cpi->denoiser.last_source, cpi->Source);
|
|
|
|
#endif
|
|
|
|
}
|