summaryrefslogtreecommitdiff
path: root/vp8/encoder
diff options
context:
space:
mode:
authorMarco Paniconi <marpan@google.com>2014-08-15 08:51:08 -0700
committerGerrit Code Review <gerrit@gerrit.golo.chromium.org>2014-08-15 08:51:08 -0700
commit715bab6e5554b1632c3c183782a9c9d01c3460cc (patch)
treee56cf6e43673f8279f84b84d13f7d7ada24e4aaf /vp8/encoder
parenta2517555007044381c8fdae9c85551d0da43ffdd (diff)
parent8db245b6a1754849ebd305f93dc011275b2d56ed (diff)
downloadlibvpx-715bab6e5554b1632c3c183782a9c9d01c3460cc.tar
libvpx-715bab6e5554b1632c3c183782a9c9d01c3460cc.tar.gz
libvpx-715bab6e5554b1632c3c183782a9c9d01c3460cc.tar.bz2
libvpx-715bab6e5554b1632c3c183782a9c9d01c3460cc.zip
Merge "Add an adaptive denoising mode."
Diffstat (limited to 'vp8/encoder')
-rw-r--r--vp8/encoder/denoising.c18
-rw-r--r--vp8/encoder/denoising.h12
-rw-r--r--vp8/encoder/onyx_if.c128
3 files changed, 155 insertions, 3 deletions
diff --git a/vp8/encoder/denoising.c b/vp8/encoder/denoising.c
index 75401fc2b..4621d13e2 100644
--- a/vp8/encoder/denoising.c
+++ b/vp8/encoder/denoising.c
@@ -341,8 +341,10 @@ void vp8_denoiser_set_parameters(VP8_DENOISER *denoiser, int mode) {
denoiser->denoiser_mode = kDenoiserOnYOnly;
} else if (mode == 2) {
denoiser->denoiser_mode = kDenoiserOnYUV;
- } else {
+ } else if (mode == 3) {
denoiser->denoiser_mode = kDenoiserOnYUVAggressive;
+ } else {
+ denoiser->denoiser_mode = kDenoiserOnAdaptive;
}
if (denoiser->denoiser_mode != kDenoiserOnYUVAggressive) {
denoiser->denoise_pars.scale_sse_thresh = 1;
@@ -397,9 +399,23 @@ int vp8_denoiser_allocate(VP8_DENOISER *denoiser, int width, int height,
vpx_memset(denoiser->yv12_mc_running_avg.buffer_alloc, 0,
denoiser->yv12_mc_running_avg.frame_size);
+ if (vp8_yv12_alloc_frame_buffer(&denoiser->yv12_last_source, width,
+ height, VP8BORDERINPIXELS) < 0) {
+ vp8_denoiser_free(denoiser);
+ return 1;
+ }
+ vpx_memset(denoiser->yv12_last_source.buffer_alloc, 0,
+ denoiser->yv12_last_source.frame_size);
+
denoiser->denoise_state = vpx_calloc((num_mb_rows * num_mb_cols), 1);
vpx_memset(denoiser->denoise_state, 0, (num_mb_rows * num_mb_cols));
vp8_denoiser_set_parameters(denoiser, mode);
+ denoiser->nmse_source_diff = 0;
+ denoiser->nmse_source_diff_count = 0;
+ // TODO(marpan): Adjust thresholds, including effect on resolution.
+ denoiser->threshold_aggressive_mode = 40;
+ if (width * height > 640 * 480)
+ denoiser->threshold_aggressive_mode = 180;
return 0;
}
diff --git a/vp8/encoder/denoising.h b/vp8/encoder/denoising.h
index 89832d3c2..e1844f18a 100644
--- a/vp8/encoder/denoising.h
+++ b/vp8/encoder/denoising.h
@@ -24,7 +24,7 @@ extern "C" {
#define SUM_DIFF_THRESHOLD_UV (96) // (8 * 8 * 1.5)
#define SUM_DIFF_THRESHOLD_HIGH_UV (8 * 8 * 2)
-#define SUM_DIFF_FROM_AVG_THRESH_UV (8 * 8 * 4)
+#define SUM_DIFF_FROM_AVG_THRESH_UV (8 * 8 * 8)
#define MOTION_MAGNITUDE_THRESHOLD_UV (8*3)
enum vp8_denoiser_decision
@@ -43,7 +43,8 @@ enum vp8_denoiser_mode {
kDenoiserOff,
kDenoiserOnYOnly,
kDenoiserOnYUV,
- kDenoiserOnYUVAggressive
+ kDenoiserOnYUVAggressive,
+ kDenoiserOnAdaptive
};
typedef struct {
@@ -72,9 +73,14 @@ typedef struct vp8_denoiser
{
YV12_BUFFER_CONFIG yv12_running_avg[MAX_REF_FRAMES];
YV12_BUFFER_CONFIG yv12_mc_running_avg;
+ // TODO(marpan): Should remove yv12_last_source and use vp8_lookahead_peak.
+ YV12_BUFFER_CONFIG yv12_last_source;
unsigned char* denoise_state;
int num_mb_cols;
int denoiser_mode;
+ int threshold_aggressive_mode;
+ int nmse_source_diff;
+ int nmse_source_diff_count;
denoise_params denoise_pars;
} VP8_DENOISER;
@@ -83,6 +89,8 @@ int vp8_denoiser_allocate(VP8_DENOISER *denoiser, int width, int height,
void vp8_denoiser_free(VP8_DENOISER *denoiser);
+void vp8_denoiser_set_parameters(VP8_DENOISER *denoiser, int mode);
+
void vp8_denoiser_denoise_mb(VP8_DENOISER *denoiser,
MACROBLOCK *x,
unsigned int best_sse,
diff --git a/vp8/encoder/onyx_if.c b/vp8/encoder/onyx_if.c
index f5080b206..93f882507 100644
--- a/vp8/encoder/onyx_if.c
+++ b/vp8/encoder/onyx_if.c
@@ -3283,12 +3283,119 @@ static void update_reference_frames(VP8_COMP *cpi)
&cpi->denoiser.yv12_running_avg[LAST_FRAME]);
}
}
+ if (cpi->oxcf.noise_sensitivity == 4)
+ vp8_yv12_copy_frame(cpi->Source, &cpi->denoiser.yv12_last_source);
}
#endif
}
+static void process_denoiser_mode_change(VP8_COMP *cpi) {
+ const VP8_COMMON *const cm = &cpi->common;
+ int i, j;
+ int total = 0;
+ int num_blocks = 0;
+ // Number of blocks skipped along row/column in computing the
+ // nmse (normalized mean square error) of source.
+ int skip = 2;
+ // Only select blocks for computing nmse that have been encoded
+ // as ZERO LAST min_consec_zero_last frames in a row.
+ int min_consec_zero_last = 10;
+ // Decision is tested for changing the denoising mode every
+ // num_mode_change times this function is called. Note that this
+ // function called every 8 frames, so (8 * num_mode_change) is number
+ // of frames where denoising mode change is tested for switch.
+ int num_mode_change = 15;
+ // Framerate factor, to compensate for larger mse at lower framerates.
+ // TODO(marpan): Adjust this factor,
+ int fac_framerate = cpi->output_framerate < 25.0f ? 80 : 100;
+ int tot_num_blocks = cm->mb_rows * cm->mb_cols;
+ int ystride = cpi->Source->y_stride;
+ unsigned char *src = cpi->Source->y_buffer;
+ unsigned char *dst = cpi->denoiser.yv12_last_source.y_buffer;
+ static const unsigned char const_source[16] = {
+ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
+ 128, 128, 128};
+
+ // Loop through the Y plane, every skip blocks along rows and columns,
+ // summing the normalized mean square error, only for blocks that have
+ // been encoded as ZEROMV LAST at least min_consec_zero_last least frames in
+ // a row and have small sum difference between current and previous frame.
+ // Normalization here is by the contrast of the current frame block.
+ for (i = 0; i < cm->Height; i += 16 * skip) {
+ int block_index_row = (i >> 4) * cm->mb_cols;
+ for (j = 0; j < cm->Width; j += 16 * skip) {
+ int index = block_index_row + (j >> 4);
+ if (cpi->consec_zero_last[index] >= min_consec_zero_last) {
+ unsigned int sse;
+ const unsigned int mse = vp8_mse16x16(src + j,
+ ystride,
+ dst + j,
+ ystride,
+ &sse);
+ const unsigned int var = vp8_variance16x16(src + j,
+ ystride,
+ dst + j,
+ ystride,
+ &sse);
+ // Only consider this block as valid for noise measurement
+ // if the sum_diff average of the current and previous frame
+ // is small (to avoid effects from lighting change).
+ if ((mse - var) < 256) {
+ const unsigned int act = vp8_variance16x16(src + j,
+ ystride,
+ const_source,
+ 0,
+ &sse);
+ if (act > 0)
+ total += mse / act;
+ num_blocks++;
+ }
+ }
+ }
+ src += 16 * skip * ystride;
+ dst += 16 * skip * ystride;
+ }
+ total = total * fac_framerate / 100;
+
+ // Only consider this frame as valid sample if we have computed nmse over
+ // at least ~1/16 blocks, and Total > 0 (Total == 0 can happen if the
+ // application inputs duplicate frames, or contrast is all zero).
+ if (total > 0 &&
+ (num_blocks > (tot_num_blocks >> 4))) {
+ // Update the recursive mean square source_diff.
+ if (cpi->denoiser.nmse_source_diff_count == 0)
+ // First sample in new interval.
+ cpi->denoiser.nmse_source_diff = total;
+ else
+ // For subsequent samples, use average with weight ~1/4 for new sample.
+ cpi->denoiser.nmse_source_diff = (int)((total >> 2) +
+ 3 * (cpi->denoiser.nmse_source_diff >> 2));
+ cpi->denoiser.nmse_source_diff_count++;
+ }
+ // Check for changing the denoiser mode, when we have obtained #samples =
+ // num_mode_change.
+ if (cpi->denoiser.nmse_source_diff_count == num_mode_change) {
+ // Check for going up: from normal to aggressive mode.
+ if ((cpi->denoiser.denoiser_mode = kDenoiserOnYUV) &&
+ (cpi->denoiser.nmse_source_diff >
+ cpi->denoiser.threshold_aggressive_mode)) {
+ vp8_denoiser_set_parameters(&cpi->denoiser, kDenoiserOnYUVAggressive);
+ } else {
+ // Check for going down: from aggressive to normal mode.
+ if ((cpi->denoiser.denoiser_mode = kDenoiserOnYUVAggressive) &&
+ (cpi->denoiser.nmse_source_diff <
+ cpi->denoiser.threshold_aggressive_mode)) {
+ vp8_denoiser_set_parameters(&cpi->denoiser, kDenoiserOnYUV);
+ }
+ }
+ // Reset metric and counter for next interval.
+ cpi->denoiser.nmse_source_diff = 0;
+ cpi->denoiser.nmse_source_diff_count = 0;
+ }
+}
+
void vp8_loopfilter_frame(VP8_COMP *cpi, VP8_COMMON *cm)
{
const FRAME_TYPE frame_type = cm->frame_type;
@@ -3445,6 +3552,12 @@ static void encode_frame_to_data_rate
{
/* Key frame from VFW/auto-keyframe/first frame */
cm->frame_type = KEY_FRAME;
+#if CONFIG_TEMPORAL_DENOISING
+ if (cpi->oxcf.noise_sensitivity == 4) {
+ // For adaptive mode, reset denoiser to normal mode on key frame.
+ vp8_denoiser_set_parameters(&cpi->denoiser, kDenoiserOnYUV);
+ }
+#endif
}
#if CONFIG_MULTI_RES_ENCODING
@@ -4472,6 +4585,21 @@ static void encode_frame_to_data_rate
cm->frame_to_show = &cm->yv12_fb[cm->new_fb_idx];
+#if CONFIG_TEMPORAL_DENOISING
+ // For the adaptive denoising mode (noise_sensitivity == 4), sample the mse
+ // of source diff (between current and previous frame), and determine if we
+ // should switch the denoiser mode. Sampling refers to computing the mse for
+ // a sub-sample of the frame (i.e., skip x blocks along row/column), and
+ // only for blocks in that set that have used ZEROMV LAST, along with some
+ // constraint on the sum diff between blocks. This process is called every
+ // ~8 frames, to further reduce complexity.
+ if (cpi->oxcf.noise_sensitivity == 4 &&
+ cpi->frames_since_key % 8 == 0 &&
+ cm->frame_type != KEY_FRAME) {
+ process_denoiser_mode_change(cpi);
+ }
+#endif
+
#if CONFIG_MULTITHREAD
if (cpi->b_multi_threaded)
{