2014-06-05 23:00:08 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2012 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.
|
|
|
|
*/
|
|
|
|
|
2014-06-13 22:58:57 +02:00
|
|
|
#include <assert.h>
|
2014-06-17 21:51:39 +02:00
|
|
|
#include <limits.h>
|
2015-10-17 01:14:22 +02:00
|
|
|
#include <math.h>
|
|
|
|
|
2015-07-22 19:40:42 +02:00
|
|
|
#include "./vpx_dsp_rtcd.h"
|
2015-08-31 23:36:35 +02:00
|
|
|
#include "vpx_dsp/vpx_dsp_common.h"
|
2014-06-05 23:00:08 +02:00
|
|
|
#include "vpx_scale/yv12config.h"
|
2014-06-13 22:58:57 +02:00
|
|
|
#include "vpx/vpx_integer.h"
|
2014-06-17 22:01:34 +02:00
|
|
|
#include "vp9/common/vp9_reconinter.h"
|
2014-07-08 17:36:25 +02:00
|
|
|
#include "vp9/encoder/vp9_context_tree.h"
|
2014-06-13 22:58:57 +02:00
|
|
|
#include "vp9/encoder/vp9_denoiser.h"
|
2015-10-17 01:14:22 +02:00
|
|
|
#include "vp9/encoder/vp9_encoder.h"
|
2014-06-05 23:00:08 +02:00
|
|
|
|
2014-06-18 17:30:07 +02:00
|
|
|
#ifdef OUTPUT_YUV_DENOISED
|
|
|
|
static void make_grayscale(YV12_BUFFER_CONFIG *yuv);
|
|
|
|
#endif
|
|
|
|
|
2014-06-17 22:04:39 +02:00
|
|
|
static int absdiff_thresh(BLOCK_SIZE bs, int increase_denoising) {
|
|
|
|
(void)bs;
|
|
|
|
return 3 + (increase_denoising ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int delta_thresh(BLOCK_SIZE bs, int increase_denoising) {
|
|
|
|
(void)bs;
|
|
|
|
(void)increase_denoising;
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int noise_motion_thresh(BLOCK_SIZE bs, int increase_denoising) {
|
|
|
|
(void)bs;
|
|
|
|
(void)increase_denoising;
|
2015-02-20 20:54:28 +01:00
|
|
|
return 625;
|
2014-06-17 22:04:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int sse_thresh(BLOCK_SIZE bs, int increase_denoising) {
|
2016-04-25 17:49:08 +02:00
|
|
|
return (1 << num_pels_log2_lookup[bs]) * (increase_denoising ? 80 : 40);
|
2014-06-17 22:04:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int sse_diff_thresh(BLOCK_SIZE bs, int increase_denoising,
|
2015-02-20 20:54:28 +01:00
|
|
|
int motion_magnitude) {
|
|
|
|
if (motion_magnitude >
|
2014-06-17 22:04:39 +02:00
|
|
|
noise_motion_thresh(bs, increase_denoising)) {
|
2016-04-25 17:49:08 +02:00
|
|
|
if (increase_denoising)
|
|
|
|
return (1 << num_pels_log2_lookup[bs]) << 2;
|
|
|
|
else
|
|
|
|
return 0;
|
2014-06-17 22:04:39 +02:00
|
|
|
} else {
|
2016-04-25 17:49:08 +02:00
|
|
|
return (1 << num_pels_log2_lookup[bs]) << 4;
|
2014-06-17 22:04:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int total_adj_weak_thresh(BLOCK_SIZE bs, int increase_denoising) {
|
2015-01-08 03:36:47 +01:00
|
|
|
return (1 << num_pels_log2_lookup[bs]) * (increase_denoising ? 3 : 2);
|
2014-06-17 22:04:39 +02:00
|
|
|
}
|
|
|
|
|
2014-09-19 01:45:53 +02:00
|
|
|
// TODO(jackychen): If increase_denoising is enabled in the future,
|
|
|
|
// we might need to update the code for calculating 'total_adj' in
|
|
|
|
// case the C code is not bit-exact with corresponding sse2 code.
|
|
|
|
int vp9_denoiser_filter_c(const uint8_t *sig, int sig_stride,
|
|
|
|
const uint8_t *mc_avg,
|
|
|
|
int mc_avg_stride,
|
|
|
|
uint8_t *avg, int avg_stride,
|
|
|
|
int increase_denoising,
|
|
|
|
BLOCK_SIZE bs,
|
|
|
|
int motion_magnitude) {
|
2014-06-12 01:22:38 +02:00
|
|
|
int r, c;
|
2014-06-17 22:04:39 +02:00
|
|
|
const uint8_t *sig_start = sig;
|
|
|
|
const uint8_t *mc_avg_start = mc_avg;
|
|
|
|
uint8_t *avg_start = avg;
|
|
|
|
int diff, adj, absdiff, delta;
|
2014-06-13 01:05:01 +02:00
|
|
|
int adj_val[] = {3, 4, 6};
|
2014-06-12 01:22:38 +02:00
|
|
|
int total_adj = 0;
|
2014-09-04 23:44:46 +02:00
|
|
|
int shift_inc = 1;
|
|
|
|
|
2014-09-10 22:19:42 +02:00
|
|
|
// If motion_magnitude is small, making the denoiser more aggressive by
|
|
|
|
// increasing the adjustment for each level. Add another increment for
|
|
|
|
// blocks that are labeled for increase denoising.
|
2014-09-04 23:44:46 +02:00
|
|
|
if (motion_magnitude <= MOTION_MAGNITUDE_THRESHOLD) {
|
|
|
|
if (increase_denoising) {
|
|
|
|
shift_inc = 2;
|
|
|
|
}
|
|
|
|
adj_val[0] += shift_inc;
|
|
|
|
adj_val[1] += shift_inc;
|
|
|
|
adj_val[2] += shift_inc;
|
|
|
|
}
|
2014-06-12 01:22:38 +02:00
|
|
|
|
2014-06-17 22:04:39 +02:00
|
|
|
// First attempt to apply a strong temporal denoising filter.
|
2014-09-19 01:45:53 +02:00
|
|
|
for (r = 0; r < (4 << b_height_log2_lookup[bs]); ++r) {
|
|
|
|
for (c = 0; c < (4 << b_width_log2_lookup[bs]); ++c) {
|
2014-06-12 01:22:38 +02:00
|
|
|
diff = mc_avg[c] - sig[c];
|
2014-06-13 01:05:01 +02:00
|
|
|
absdiff = abs(diff);
|
2014-06-12 01:22:38 +02:00
|
|
|
|
2014-06-17 22:04:39 +02:00
|
|
|
if (absdiff <= absdiff_thresh(bs, increase_denoising)) {
|
2014-06-13 01:05:01 +02:00
|
|
|
avg[c] = mc_avg[c];
|
|
|
|
total_adj += diff;
|
2014-06-12 01:22:38 +02:00
|
|
|
} else {
|
2014-06-13 01:05:01 +02:00
|
|
|
switch (absdiff) {
|
|
|
|
case 4: case 5: case 6: case 7:
|
|
|
|
adj = adj_val[0];
|
2014-06-13 22:58:57 +02:00
|
|
|
break;
|
2014-06-13 01:05:01 +02:00
|
|
|
case 8: case 9: case 10: case 11:
|
|
|
|
case 12: case 13: case 14: case 15:
|
|
|
|
adj = adj_val[1];
|
2014-06-13 22:58:57 +02:00
|
|
|
break;
|
2014-06-13 01:05:01 +02:00
|
|
|
default:
|
|
|
|
adj = adj_val[2];
|
|
|
|
}
|
|
|
|
if (diff > 0) {
|
2015-08-18 03:19:22 +02:00
|
|
|
avg[c] = VPXMIN(UINT8_MAX, sig[c] + adj);
|
2014-06-13 01:05:01 +02:00
|
|
|
total_adj += adj;
|
|
|
|
} else {
|
2015-08-18 03:19:22 +02:00
|
|
|
avg[c] = VPXMAX(0, sig[c] - adj);
|
2014-06-13 01:05:01 +02:00
|
|
|
total_adj -= adj;
|
|
|
|
}
|
2014-06-12 01:22:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
sig += sig_stride;
|
|
|
|
avg += avg_stride;
|
|
|
|
mc_avg += mc_avg_stride;
|
|
|
|
}
|
2014-06-17 22:04:39 +02:00
|
|
|
|
|
|
|
// If the strong filter did not modify the signal too much, we're all set.
|
|
|
|
if (abs(total_adj) <= total_adj_strong_thresh(bs, increase_denoising)) {
|
|
|
|
return FILTER_BLOCK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we try to dampen the filter if the delta is not too high.
|
|
|
|
delta = ((abs(total_adj) - total_adj_strong_thresh(bs, increase_denoising))
|
2014-09-29 20:30:17 +02:00
|
|
|
>> num_pels_log2_lookup[bs]) + 1;
|
2014-09-04 23:08:18 +02:00
|
|
|
|
|
|
|
if (delta >= delta_thresh(bs, increase_denoising)) {
|
2014-06-17 22:04:39 +02:00
|
|
|
return COPY_BLOCK;
|
|
|
|
}
|
|
|
|
|
|
|
|
mc_avg = mc_avg_start;
|
|
|
|
avg = avg_start;
|
|
|
|
sig = sig_start;
|
2014-09-19 01:45:53 +02:00
|
|
|
for (r = 0; r < (4 << b_height_log2_lookup[bs]); ++r) {
|
|
|
|
for (c = 0; c < (4 << b_width_log2_lookup[bs]); ++c) {
|
2014-06-17 22:04:39 +02:00
|
|
|
diff = mc_avg[c] - sig[c];
|
|
|
|
adj = abs(diff);
|
|
|
|
if (adj > delta) {
|
|
|
|
adj = delta;
|
|
|
|
}
|
|
|
|
if (diff > 0) {
|
2014-09-04 20:16:56 +02:00
|
|
|
// Diff positive means we made positive adjustment above
|
|
|
|
// (in first try/attempt), so now make negative adjustment to bring
|
|
|
|
// denoised signal down.
|
2015-08-18 03:19:22 +02:00
|
|
|
avg[c] = VPXMAX(0, avg[c] - adj);
|
2014-09-04 20:16:56 +02:00
|
|
|
total_adj -= adj;
|
2014-06-17 22:04:39 +02:00
|
|
|
} else {
|
2014-09-04 20:16:56 +02:00
|
|
|
// Diff negative means we made negative adjustment above
|
|
|
|
// (in first try/attempt), so now make positive adjustment to bring
|
|
|
|
// denoised signal up.
|
2015-08-18 03:19:22 +02:00
|
|
|
avg[c] = VPXMIN(UINT8_MAX, avg[c] + adj);
|
2014-09-04 20:16:56 +02:00
|
|
|
total_adj += adj;
|
2014-06-17 22:04:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
sig += sig_stride;
|
|
|
|
avg += avg_stride;
|
|
|
|
mc_avg += mc_avg_stride;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We can use the filter if it has been sufficiently dampened
|
|
|
|
if (abs(total_adj) <= total_adj_weak_thresh(bs, increase_denoising)) {
|
|
|
|
return FILTER_BLOCK;
|
|
|
|
}
|
|
|
|
return COPY_BLOCK;
|
2014-06-12 01:22:38 +02:00
|
|
|
}
|
|
|
|
|
2014-06-13 22:58:57 +02:00
|
|
|
static uint8_t *block_start(uint8_t *framebuf, int stride,
|
|
|
|
int mi_row, int mi_col) {
|
2016-04-04 18:11:00 +02:00
|
|
|
return framebuf + (stride * mi_row << 3) + (mi_col << 3);
|
2014-06-12 23:45:42 +02:00
|
|
|
}
|
|
|
|
|
2014-06-18 18:05:32 +02:00
|
|
|
static VP9_DENOISER_DECISION perform_motion_compensation(VP9_DENOISER *denoiser,
|
|
|
|
MACROBLOCK *mb,
|
|
|
|
BLOCK_SIZE bs,
|
|
|
|
int increase_denoising,
|
|
|
|
int mi_row,
|
2014-07-08 17:36:25 +02:00
|
|
|
int mi_col,
|
2014-09-04 23:44:46 +02:00
|
|
|
PICK_MODE_CONTEXT *ctx,
|
2015-10-27 23:50:06 +01:00
|
|
|
int motion_magnitude,
|
2015-12-01 18:30:35 +01:00
|
|
|
int is_skin,
|
2016-04-01 20:41:29 +02:00
|
|
|
int *zeromv_filter,
|
|
|
|
int consec_zeromv) {
|
2014-07-08 17:36:25 +02:00
|
|
|
int sse_diff = ctx->zeromv_sse - ctx->newmv_sse;
|
2014-06-17 22:01:34 +02:00
|
|
|
MV_REFERENCE_FRAME frame;
|
|
|
|
MACROBLOCKD *filter_mbd = &mb->e_mbd;
|
2016-01-20 01:40:20 +01:00
|
|
|
MODE_INFO *mi = filter_mbd->mi[0];
|
|
|
|
MODE_INFO saved_mi;
|
2016-04-04 18:11:00 +02:00
|
|
|
int i;
|
2014-07-07 20:35:27 +02:00
|
|
|
struct buf_2d saved_dst[MAX_MB_PLANE];
|
2016-04-04 18:11:00 +02:00
|
|
|
struct buf_2d saved_pre[MAX_MB_PLANE];
|
2014-07-07 20:35:27 +02:00
|
|
|
|
2014-07-08 17:36:25 +02:00
|
|
|
frame = ctx->best_reference_frame;
|
2016-01-20 01:40:20 +01:00
|
|
|
saved_mi = *mi;
|
2015-01-08 21:15:30 +01:00
|
|
|
|
2016-04-01 20:41:29 +02:00
|
|
|
if (is_skin && (motion_magnitude > 0 || consec_zeromv < 4))
|
2015-10-08 00:08:20 +02:00
|
|
|
return COPY_BLOCK;
|
|
|
|
|
2016-04-04 18:25:59 +02:00
|
|
|
// Avoid denoising for small block (unless motion is small).
|
|
|
|
// Small blocks are selected in variance partition (before encoding) and
|
|
|
|
// will typically lie on moving areas.
|
2016-04-22 19:30:21 +02:00
|
|
|
if (denoiser->denoising_level < kDenHigh &&
|
|
|
|
motion_magnitude > 16 && bs <= BLOCK_8X8)
|
2016-04-04 18:25:59 +02:00
|
|
|
return COPY_BLOCK;
|
|
|
|
|
2014-06-17 22:01:34 +02:00
|
|
|
// If the best reference frame uses inter-prediction and there is enough of a
|
|
|
|
// difference in sum-squared-error, use it.
|
2014-06-17 22:04:39 +02:00
|
|
|
if (frame != INTRA_FRAME &&
|
2016-04-25 17:49:08 +02:00
|
|
|
ctx->newmv_sse != UINT_MAX &&
|
2015-10-27 23:50:06 +01:00
|
|
|
sse_diff > sse_diff_thresh(bs, increase_denoising, motion_magnitude)) {
|
2016-01-20 01:40:20 +01:00
|
|
|
mi->ref_frame[0] = ctx->best_reference_frame;
|
|
|
|
mi->mode = ctx->best_sse_inter_mode;
|
|
|
|
mi->mv[0] = ctx->best_sse_mv;
|
2014-06-17 22:01:34 +02:00
|
|
|
} else {
|
|
|
|
// Otherwise, use the zero reference frame.
|
2014-07-08 17:36:25 +02:00
|
|
|
frame = ctx->best_zeromv_reference_frame;
|
2015-11-19 02:04:15 +01:00
|
|
|
ctx->newmv_sse = ctx->zeromv_sse;
|
|
|
|
// Bias to last reference.
|
|
|
|
if (frame != LAST_FRAME &&
|
|
|
|
((ctx->zeromv_lastref_sse < (5 * ctx->zeromv_sse) >> 2) ||
|
|
|
|
denoiser->denoising_level >= kDenHigh)) {
|
|
|
|
frame = LAST_FRAME;
|
|
|
|
ctx->newmv_sse = ctx->zeromv_lastref_sse;
|
|
|
|
}
|
2016-01-20 01:40:20 +01:00
|
|
|
mi->ref_frame[0] = frame;
|
|
|
|
mi->mode = ZEROMV;
|
|
|
|
mi->mv[0].as_int = 0;
|
2014-07-08 17:36:25 +02:00
|
|
|
ctx->best_sse_inter_mode = ZEROMV;
|
|
|
|
ctx->best_sse_mv.as_int = 0;
|
2015-12-01 18:30:35 +01:00
|
|
|
*zeromv_filter = 1;
|
2016-04-22 19:30:21 +02:00
|
|
|
if (denoiser->denoising_level > kDenMedium) {
|
|
|
|
motion_magnitude = 0;
|
|
|
|
}
|
2014-06-17 22:01:34 +02:00
|
|
|
}
|
|
|
|
|
2015-01-08 21:15:30 +01:00
|
|
|
if (ctx->newmv_sse > sse_thresh(bs, increase_denoising)) {
|
|
|
|
// Restore everything to its original state
|
2016-01-20 01:40:20 +01:00
|
|
|
*mi = saved_mi;
|
2015-01-08 21:15:30 +01:00
|
|
|
return COPY_BLOCK;
|
|
|
|
}
|
2015-10-27 23:50:06 +01:00
|
|
|
if (motion_magnitude >
|
2015-02-20 20:54:28 +01:00
|
|
|
(noise_motion_thresh(bs, increase_denoising) << 3)) {
|
2015-01-08 21:15:30 +01:00
|
|
|
// Restore everything to its original state
|
2016-01-20 01:40:20 +01:00
|
|
|
*mi = saved_mi;
|
2015-01-08 21:15:30 +01:00
|
|
|
return COPY_BLOCK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We will restore these after motion compensation.
|
|
|
|
for (i = 0; i < MAX_MB_PLANE; ++i) {
|
2016-04-04 18:11:00 +02:00
|
|
|
saved_pre[i] = filter_mbd->plane[i].pre[0];
|
2015-01-08 21:15:30 +01:00
|
|
|
saved_dst[i] = filter_mbd->plane[i].dst;
|
|
|
|
}
|
|
|
|
|
2014-06-17 22:01:34 +02:00
|
|
|
// Set the pointers in the MACROBLOCKD to point to the buffers in the denoiser
|
|
|
|
// struct.
|
2016-04-04 18:11:00 +02:00
|
|
|
filter_mbd->plane[0].pre[0].buf =
|
|
|
|
block_start(denoiser->running_avg_y[frame].y_buffer,
|
|
|
|
denoiser->running_avg_y[frame].y_stride,
|
|
|
|
mi_row, mi_col);
|
|
|
|
filter_mbd->plane[0].pre[0].stride =
|
|
|
|
denoiser->running_avg_y[frame].y_stride;
|
|
|
|
filter_mbd->plane[1].pre[0].buf =
|
|
|
|
block_start(denoiser->running_avg_y[frame].u_buffer,
|
|
|
|
denoiser->running_avg_y[frame].uv_stride,
|
|
|
|
mi_row, mi_col);
|
|
|
|
filter_mbd->plane[1].pre[0].stride =
|
|
|
|
denoiser->running_avg_y[frame].uv_stride;
|
|
|
|
filter_mbd->plane[2].pre[0].buf =
|
|
|
|
block_start(denoiser->running_avg_y[frame].v_buffer,
|
|
|
|
denoiser->running_avg_y[frame].uv_stride,
|
|
|
|
mi_row, mi_col);
|
|
|
|
filter_mbd->plane[2].pre[0].stride =
|
|
|
|
denoiser->running_avg_y[frame].uv_stride;
|
|
|
|
|
2014-06-17 22:01:34 +02:00
|
|
|
filter_mbd->plane[0].dst.buf =
|
|
|
|
block_start(denoiser->mc_running_avg_y.y_buffer,
|
|
|
|
denoiser->mc_running_avg_y.y_stride,
|
|
|
|
mi_row, mi_col);
|
|
|
|
filter_mbd->plane[0].dst.stride = denoiser->mc_running_avg_y.y_stride;
|
|
|
|
filter_mbd->plane[1].dst.buf =
|
|
|
|
block_start(denoiser->mc_running_avg_y.u_buffer,
|
|
|
|
denoiser->mc_running_avg_y.uv_stride,
|
|
|
|
mi_row, mi_col);
|
2014-07-07 20:35:27 +02:00
|
|
|
filter_mbd->plane[1].dst.stride = denoiser->mc_running_avg_y.uv_stride;
|
2014-06-17 22:01:34 +02:00
|
|
|
filter_mbd->plane[2].dst.buf =
|
|
|
|
block_start(denoiser->mc_running_avg_y.v_buffer,
|
|
|
|
denoiser->mc_running_avg_y.uv_stride,
|
|
|
|
mi_row, mi_col);
|
2014-07-07 20:35:27 +02:00
|
|
|
filter_mbd->plane[2].dst.stride = denoiser->mc_running_avg_y.uv_stride;
|
2014-06-17 22:01:34 +02:00
|
|
|
|
2016-04-04 18:11:00 +02:00
|
|
|
vp9_build_inter_predictors_sby(filter_mbd, mi_row, mi_col, bs);
|
2014-06-17 22:01:34 +02:00
|
|
|
|
|
|
|
// Restore everything to its original state
|
2016-01-20 01:40:20 +01:00
|
|
|
*mi = saved_mi;
|
2014-07-07 20:35:27 +02:00
|
|
|
for (i = 0; i < MAX_MB_PLANE; ++i) {
|
2016-04-04 18:11:00 +02:00
|
|
|
filter_mbd->plane[i].pre[0] = saved_pre[i];
|
2014-07-07 20:35:27 +02:00
|
|
|
filter_mbd->plane[i].dst = saved_dst[i];
|
|
|
|
}
|
2014-06-17 22:01:34 +02:00
|
|
|
|
|
|
|
return FILTER_BLOCK;
|
|
|
|
}
|
|
|
|
|
2016-03-24 23:29:58 +01:00
|
|
|
void vp9_denoiser_denoise(VP9_COMP *cpi, MACROBLOCK *mb,
|
2014-07-08 17:36:25 +02:00
|
|
|
int mi_row, int mi_col, BLOCK_SIZE bs,
|
2015-11-14 00:58:42 +01:00
|
|
|
PICK_MODE_CONTEXT *ctx,
|
|
|
|
VP9_DENOISER_DECISION *denoiser_decision) {
|
2015-10-27 23:50:06 +01:00
|
|
|
int mv_col, mv_row;
|
2014-09-04 23:44:46 +02:00
|
|
|
int motion_magnitude = 0;
|
2015-12-01 18:30:35 +01:00
|
|
|
int zeromv_filter = 0;
|
2016-03-24 23:29:58 +01:00
|
|
|
VP9_DENOISER *denoiser = &cpi->denoiser;
|
2015-10-08 00:08:20 +02:00
|
|
|
VP9_DENOISER_DECISION decision = COPY_BLOCK;
|
2014-06-12 23:45:42 +02:00
|
|
|
YV12_BUFFER_CONFIG avg = denoiser->running_avg_y[INTRA_FRAME];
|
2014-06-14 01:02:36 +02:00
|
|
|
YV12_BUFFER_CONFIG mc_avg = denoiser->mc_running_avg_y;
|
|
|
|
uint8_t *avg_start = block_start(avg.y_buffer, avg.y_stride, mi_row, mi_col);
|
|
|
|
uint8_t *mc_avg_start = block_start(mc_avg.y_buffer, mc_avg.y_stride,
|
|
|
|
mi_row, mi_col);
|
2014-06-12 23:45:42 +02:00
|
|
|
struct buf_2d src = mb->plane[0].src;
|
2015-10-08 00:08:20 +02:00
|
|
|
int is_skin = 0;
|
2016-04-01 20:41:29 +02:00
|
|
|
int consec_zeromv = 0;
|
2016-03-24 23:29:58 +01:00
|
|
|
mv_col = ctx->best_sse_mv.as_mv.col;
|
|
|
|
mv_row = ctx->best_sse_mv.as_mv.row;
|
|
|
|
motion_magnitude = mv_row * mv_row + mv_col * mv_col;
|
2015-10-08 00:08:20 +02:00
|
|
|
|
2016-03-24 23:29:58 +01:00
|
|
|
if (cpi->use_skin_detection &&
|
|
|
|
bs <= BLOCK_32X32 &&
|
2016-04-22 19:30:21 +02:00
|
|
|
denoiser->denoising_level < kDenHigh) {
|
2016-03-24 23:29:58 +01:00
|
|
|
int motion_level = (motion_magnitude < 16) ? 0 : 1;
|
|
|
|
// If motion for current block is small/zero, compute consec_zeromv for
|
|
|
|
// skin detection (early exit in skin detection is done for large
|
|
|
|
// consec_zeromv when current block has small/zero motion).
|
2016-04-01 20:41:29 +02:00
|
|
|
consec_zeromv = 0;
|
2016-03-24 23:29:58 +01:00
|
|
|
if (motion_level == 0) {
|
|
|
|
VP9_COMMON * const cm = &cpi->common;
|
|
|
|
int j, i;
|
|
|
|
// Loop through the 8x8 sub-blocks.
|
|
|
|
const int bw = num_8x8_blocks_wide_lookup[BLOCK_64X64];
|
|
|
|
const int bh = num_8x8_blocks_high_lookup[BLOCK_64X64];
|
|
|
|
const int xmis = VPXMIN(cm->mi_cols - mi_col, bw);
|
|
|
|
const int ymis = VPXMIN(cm->mi_rows - mi_row, bh);
|
|
|
|
const int block_index = mi_row * cm->mi_cols + mi_col;
|
|
|
|
consec_zeromv = 100;
|
|
|
|
for (i = 0; i < ymis; i++) {
|
|
|
|
for (j = 0; j < xmis; j++) {
|
|
|
|
int bl_index = block_index + i * cm->mi_cols + j;
|
2016-04-21 23:00:26 +02:00
|
|
|
consec_zeromv = VPXMIN(cpi->consec_zero_mv[bl_index], consec_zeromv);
|
2016-03-24 23:29:58 +01:00
|
|
|
// No need to keep checking 8x8 blocks if any of the sub-blocks
|
|
|
|
// has small consec_zeromv (since threshold for no_skin based on
|
2016-04-01 20:41:29 +02:00
|
|
|
// zero/small motion in skin detection is high, i.e, > 4).
|
|
|
|
if (consec_zeromv < 4) {
|
2016-03-24 23:29:58 +01:00
|
|
|
i = ymis;
|
|
|
|
j = xmis;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO(marpan): Compute skin detection over sub-blocks.
|
2016-01-05 01:48:15 +01:00
|
|
|
is_skin = vp9_compute_skin_block(mb->plane[0].src.buf,
|
|
|
|
mb->plane[1].src.buf,
|
|
|
|
mb->plane[2].src.buf,
|
|
|
|
mb->plane[0].src.stride,
|
|
|
|
mb->plane[1].src.stride,
|
2016-03-14 19:14:41 +01:00
|
|
|
bs,
|
2016-03-24 23:29:58 +01:00
|
|
|
consec_zeromv,
|
|
|
|
motion_level);
|
2015-10-08 00:08:20 +02:00
|
|
|
}
|
2016-01-29 17:36:03 +01:00
|
|
|
if (!is_skin &&
|
2016-04-22 19:30:21 +02:00
|
|
|
denoiser->denoising_level == kDenHigh) {
|
2015-10-27 23:50:06 +01:00
|
|
|
denoiser->increase_denoising = 1;
|
|
|
|
} else {
|
|
|
|
denoiser->increase_denoising = 0;
|
|
|
|
}
|
|
|
|
|
2015-11-11 19:09:40 +01:00
|
|
|
if (denoiser->denoising_level >= kDenLow)
|
2015-10-17 01:14:22 +02:00
|
|
|
decision = perform_motion_compensation(denoiser, mb, bs,
|
|
|
|
denoiser->increase_denoising,
|
|
|
|
mi_row, mi_col, ctx,
|
2015-10-27 23:50:06 +01:00
|
|
|
motion_magnitude,
|
2015-12-01 18:30:35 +01:00
|
|
|
is_skin,
|
2016-04-01 20:41:29 +02:00
|
|
|
&zeromv_filter,
|
|
|
|
consec_zeromv);
|
2014-06-12 23:45:42 +02:00
|
|
|
|
|
|
|
if (decision == FILTER_BLOCK) {
|
2014-09-19 01:45:53 +02:00
|
|
|
decision = vp9_denoiser_filter(src.buf, src.stride,
|
|
|
|
mc_avg_start, mc_avg.y_stride,
|
|
|
|
avg_start, avg.y_stride,
|
2015-10-27 23:50:06 +01:00
|
|
|
denoiser->increase_denoising,
|
|
|
|
bs, motion_magnitude);
|
2014-06-12 23:45:42 +02:00
|
|
|
}
|
2014-06-17 22:04:39 +02:00
|
|
|
|
|
|
|
if (decision == FILTER_BLOCK) {
|
2015-07-22 19:40:42 +02:00
|
|
|
vpx_convolve_copy(avg_start, avg.y_stride, src.buf, src.stride,
|
2015-01-08 03:20:38 +01:00
|
|
|
NULL, 0, NULL, 0,
|
|
|
|
num_4x4_blocks_wide_lookup[bs] << 2,
|
|
|
|
num_4x4_blocks_high_lookup[bs] << 2);
|
2014-06-17 22:04:39 +02:00
|
|
|
} else { // COPY_BLOCK
|
2015-07-22 19:40:42 +02:00
|
|
|
vpx_convolve_copy(src.buf, src.stride, avg_start, avg.y_stride,
|
2015-01-08 03:20:38 +01:00
|
|
|
NULL, 0, NULL, 0,
|
|
|
|
num_4x4_blocks_wide_lookup[bs] << 2,
|
|
|
|
num_4x4_blocks_high_lookup[bs] << 2);
|
2014-06-12 23:45:42 +02:00
|
|
|
}
|
2015-11-14 00:58:42 +01:00
|
|
|
*denoiser_decision = decision;
|
2015-12-01 18:30:35 +01:00
|
|
|
if (decision == FILTER_BLOCK && zeromv_filter == 1)
|
|
|
|
*denoiser_decision = FILTER_ZEROMV_BLOCK;
|
2014-06-05 23:00:08 +02:00
|
|
|
}
|
|
|
|
|
2015-10-07 22:21:25 +02:00
|
|
|
static void copy_frame(YV12_BUFFER_CONFIG * const dest,
|
|
|
|
const YV12_BUFFER_CONFIG * const src) {
|
2014-07-01 17:31:06 +02:00
|
|
|
int r;
|
2015-10-07 22:21:25 +02:00
|
|
|
const uint8_t *srcbuf = src->y_buffer;
|
|
|
|
uint8_t *destbuf = dest->y_buffer;
|
2015-01-16 18:03:40 +01:00
|
|
|
|
2015-10-07 22:21:25 +02:00
|
|
|
assert(dest->y_width == src->y_width);
|
|
|
|
assert(dest->y_height == src->y_height);
|
2014-06-13 21:43:52 +02:00
|
|
|
|
2015-10-07 22:21:25 +02:00
|
|
|
for (r = 0; r < dest->y_height; ++r) {
|
|
|
|
memcpy(destbuf, srcbuf, dest->y_width);
|
|
|
|
destbuf += dest->y_stride;
|
|
|
|
srcbuf += src->y_stride;
|
2014-06-13 21:43:52 +02:00
|
|
|
}
|
2014-06-12 22:53:49 +02:00
|
|
|
}
|
|
|
|
|
2015-10-07 22:21:25 +02:00
|
|
|
static void swap_frame_buffer(YV12_BUFFER_CONFIG * const dest,
|
|
|
|
YV12_BUFFER_CONFIG * const src) {
|
2015-01-16 18:03:40 +01:00
|
|
|
uint8_t *tmp_buf = dest->y_buffer;
|
|
|
|
assert(dest->y_width == src->y_width);
|
|
|
|
assert(dest->y_height == src->y_height);
|
|
|
|
dest->y_buffer = src->y_buffer;
|
|
|
|
src->y_buffer = tmp_buf;
|
2015-01-08 03:30:50 +01:00
|
|
|
}
|
|
|
|
|
2014-06-05 23:00:08 +02:00
|
|
|
void vp9_denoiser_update_frame_info(VP9_DENOISER *denoiser,
|
2014-06-12 22:53:49 +02:00
|
|
|
YV12_BUFFER_CONFIG src,
|
2014-06-05 23:00:08 +02:00
|
|
|
FRAME_TYPE frame_type,
|
|
|
|
int refresh_alt_ref_frame,
|
|
|
|
int refresh_golden_frame,
|
2015-10-02 02:31:40 +02:00
|
|
|
int refresh_last_frame,
|
|
|
|
int resized) {
|
|
|
|
// Copy source into denoised reference buffers on KEY_FRAME or
|
|
|
|
// if the just encoded frame was resized.
|
2016-04-04 20:05:21 +02:00
|
|
|
if (frame_type == KEY_FRAME || resized != 0 || denoiser->reset) {
|
2014-06-13 22:58:57 +02:00
|
|
|
int i;
|
2014-07-18 23:15:30 +02:00
|
|
|
// Start at 1 so as not to overwrite the INTRA_FRAME
|
2015-01-08 03:30:50 +01:00
|
|
|
for (i = 1; i < MAX_REF_FRAMES; ++i)
|
2015-10-07 22:21:25 +02:00
|
|
|
copy_frame(&denoiser->running_avg_y[i], &src);
|
2016-04-04 20:05:21 +02:00
|
|
|
denoiser->reset = 0;
|
2015-01-08 03:30:50 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-07 19:44:04 +02:00
|
|
|
// If more than one refresh occurs, must copy frame buffer.
|
|
|
|
if ((refresh_alt_ref_frame + refresh_golden_frame + refresh_last_frame)
|
|
|
|
> 1) {
|
|
|
|
if (refresh_alt_ref_frame) {
|
2015-10-07 22:21:25 +02:00
|
|
|
copy_frame(&denoiser->running_avg_y[ALTREF_FRAME],
|
|
|
|
&denoiser->running_avg_y[INTRA_FRAME]);
|
2015-10-07 19:44:04 +02:00
|
|
|
}
|
|
|
|
if (refresh_golden_frame) {
|
2015-10-07 22:21:25 +02:00
|
|
|
copy_frame(&denoiser->running_avg_y[GOLDEN_FRAME],
|
|
|
|
&denoiser->running_avg_y[INTRA_FRAME]);
|
2015-10-07 19:44:04 +02:00
|
|
|
}
|
|
|
|
if (refresh_last_frame) {
|
2015-10-07 22:21:25 +02:00
|
|
|
copy_frame(&denoiser->running_avg_y[LAST_FRAME],
|
|
|
|
&denoiser->running_avg_y[INTRA_FRAME]);
|
2015-10-07 19:44:04 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (refresh_alt_ref_frame) {
|
|
|
|
swap_frame_buffer(&denoiser->running_avg_y[ALTREF_FRAME],
|
|
|
|
&denoiser->running_avg_y[INTRA_FRAME]);
|
|
|
|
}
|
|
|
|
if (refresh_golden_frame) {
|
|
|
|
swap_frame_buffer(&denoiser->running_avg_y[GOLDEN_FRAME],
|
|
|
|
&denoiser->running_avg_y[INTRA_FRAME]);
|
|
|
|
}
|
|
|
|
if (refresh_last_frame) {
|
|
|
|
swap_frame_buffer(&denoiser->running_avg_y[LAST_FRAME],
|
|
|
|
&denoiser->running_avg_y[INTRA_FRAME]);
|
|
|
|
}
|
2014-06-12 22:53:49 +02:00
|
|
|
}
|
2014-06-05 23:00:08 +02:00
|
|
|
}
|
|
|
|
|
2014-07-08 17:36:25 +02:00
|
|
|
void vp9_denoiser_reset_frame_stats(PICK_MODE_CONTEXT *ctx) {
|
|
|
|
ctx->zeromv_sse = UINT_MAX;
|
2015-01-28 19:24:25 +01:00
|
|
|
ctx->newmv_sse = UINT_MAX;
|
2015-11-25 00:56:59 +01:00
|
|
|
ctx->zeromv_lastref_sse = UINT_MAX;
|
2016-04-25 20:44:40 +02:00
|
|
|
ctx->best_sse_mv.as_int = 0;
|
2014-06-17 21:51:39 +02:00
|
|
|
}
|
|
|
|
|
2016-01-20 01:40:20 +01:00
|
|
|
void vp9_denoiser_update_frame_stats(MODE_INFO *mi, unsigned int sse,
|
2014-08-22 21:17:40 +02:00
|
|
|
PREDICTION_MODE mode,
|
2014-07-08 17:36:25 +02:00
|
|
|
PICK_MODE_CONTEXT *ctx) {
|
2016-01-20 01:40:20 +01:00
|
|
|
if (mi->mv[0].as_int == 0 && sse < ctx->zeromv_sse) {
|
2014-07-08 17:36:25 +02:00
|
|
|
ctx->zeromv_sse = sse;
|
2016-01-20 01:40:20 +01:00
|
|
|
ctx->best_zeromv_reference_frame = mi->ref_frame[0];
|
|
|
|
if (mi->ref_frame[0] == LAST_FRAME)
|
2015-11-19 02:04:15 +01:00
|
|
|
ctx->zeromv_lastref_sse = sse;
|
2014-06-17 21:51:39 +02:00
|
|
|
}
|
|
|
|
|
2016-01-20 01:40:20 +01:00
|
|
|
if (mi->mv[0].as_int != 0 && sse < ctx->newmv_sse) {
|
2014-07-08 17:36:25 +02:00
|
|
|
ctx->newmv_sse = sse;
|
|
|
|
ctx->best_sse_inter_mode = mode;
|
2016-01-20 01:40:20 +01:00
|
|
|
ctx->best_sse_mv = mi->mv[0];
|
|
|
|
ctx->best_reference_frame = mi->ref_frame[0];
|
2014-06-17 21:51:39 +02:00
|
|
|
}
|
2014-06-05 23:00:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int vp9_denoiser_alloc(VP9_DENOISER *denoiser, int width, int height,
|
2014-08-26 21:35:15 +02:00
|
|
|
int ssx, int ssy,
|
|
|
|
#if CONFIG_VP9_HIGHBITDEPTH
|
|
|
|
int use_highbitdepth,
|
|
|
|
#endif
|
|
|
|
int border) {
|
2014-06-12 22:53:49 +02:00
|
|
|
int i, fail;
|
2014-12-15 21:00:09 +01:00
|
|
|
const int legacy_byte_alignment = 0;
|
2014-06-13 22:58:57 +02:00
|
|
|
assert(denoiser != NULL);
|
2014-06-12 01:12:21 +02:00
|
|
|
|
2014-06-12 22:53:49 +02:00
|
|
|
for (i = 0; i < MAX_REF_FRAMES; ++i) {
|
2015-08-15 00:27:02 +02:00
|
|
|
fail = vpx_alloc_frame_buffer(&denoiser->running_avg_y[i], width, height,
|
2014-08-26 21:35:15 +02:00
|
|
|
ssx, ssy,
|
|
|
|
#if CONFIG_VP9_HIGHBITDEPTH
|
|
|
|
use_highbitdepth,
|
|
|
|
#endif
|
2014-12-15 21:00:09 +01:00
|
|
|
border, legacy_byte_alignment);
|
2014-06-12 22:53:49 +02:00
|
|
|
if (fail) {
|
|
|
|
vp9_denoiser_free(denoiser);
|
|
|
|
return 1;
|
|
|
|
}
|
2014-06-18 17:30:07 +02:00
|
|
|
#ifdef OUTPUT_YUV_DENOISED
|
|
|
|
make_grayscale(&denoiser->running_avg_y[i]);
|
|
|
|
#endif
|
2014-06-12 01:12:21 +02:00
|
|
|
}
|
|
|
|
|
2015-08-15 00:27:02 +02:00
|
|
|
fail = vpx_alloc_frame_buffer(&denoiser->mc_running_avg_y, width, height,
|
2014-08-26 21:35:15 +02:00
|
|
|
ssx, ssy,
|
|
|
|
#if CONFIG_VP9_HIGHBITDEPTH
|
|
|
|
use_highbitdepth,
|
2015-10-17 01:14:22 +02:00
|
|
|
#endif
|
|
|
|
border, legacy_byte_alignment);
|
|
|
|
if (fail) {
|
|
|
|
vp9_denoiser_free(denoiser);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fail = vpx_alloc_frame_buffer(&denoiser->last_source, width, height,
|
|
|
|
ssx, ssy,
|
|
|
|
#if CONFIG_VP9_HIGHBITDEPTH
|
|
|
|
use_highbitdepth,
|
2014-08-26 21:35:15 +02:00
|
|
|
#endif
|
2014-12-15 21:00:09 +01:00
|
|
|
border, legacy_byte_alignment);
|
2014-06-12 22:53:49 +02:00
|
|
|
if (fail) {
|
2014-06-12 01:12:21 +02:00
|
|
|
vp9_denoiser_free(denoiser);
|
|
|
|
return 1;
|
|
|
|
}
|
2014-06-18 17:30:07 +02:00
|
|
|
#ifdef OUTPUT_YUV_DENOISED
|
|
|
|
make_grayscale(&denoiser->running_avg_y[i]);
|
|
|
|
#endif
|
2014-06-17 21:51:39 +02:00
|
|
|
denoiser->increase_denoising = 0;
|
2015-01-03 21:00:46 +01:00
|
|
|
denoiser->frame_buffer_initialized = 1;
|
2015-11-11 19:09:40 +01:00
|
|
|
denoiser->denoising_level = kDenLow;
|
2016-04-04 20:05:21 +02:00
|
|
|
denoiser->prev_denoising_level = kDenLow;
|
|
|
|
denoiser->reset = 0;
|
2014-06-05 23:00:08 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void vp9_denoiser_free(VP9_DENOISER *denoiser) {
|
2014-06-12 22:53:49 +02:00
|
|
|
int i;
|
2015-01-03 21:00:46 +01:00
|
|
|
denoiser->frame_buffer_initialized = 0;
|
2014-06-13 22:58:57 +02:00
|
|
|
if (denoiser == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2014-06-12 22:53:49 +02:00
|
|
|
for (i = 0; i < MAX_REF_FRAMES; ++i) {
|
2015-08-15 00:27:02 +02:00
|
|
|
vpx_free_frame_buffer(&denoiser->running_avg_y[i]);
|
2014-06-12 01:12:21 +02:00
|
|
|
}
|
2015-08-15 00:27:02 +02:00
|
|
|
vpx_free_frame_buffer(&denoiser->mc_running_avg_y);
|
2015-10-17 01:14:22 +02:00
|
|
|
vpx_free_frame_buffer(&denoiser->last_source);
|
|
|
|
}
|
|
|
|
|
2015-11-01 23:40:05 +01:00
|
|
|
void vp9_denoiser_set_noise_level(VP9_DENOISER *denoiser,
|
|
|
|
int noise_level) {
|
|
|
|
denoiser->denoising_level = noise_level;
|
2016-04-04 20:05:21 +02:00
|
|
|
if (denoiser->denoising_level > kDenLowLow &&
|
|
|
|
denoiser->prev_denoising_level == kDenLowLow)
|
|
|
|
denoiser->reset = 1;
|
|
|
|
else
|
|
|
|
denoiser->reset = 0;
|
|
|
|
denoiser->prev_denoising_level = denoiser->denoising_level;
|
2014-06-05 23:00:08 +02:00
|
|
|
}
|
2014-06-18 17:30:07 +02:00
|
|
|
|
|
|
|
#ifdef OUTPUT_YUV_DENOISED
|
|
|
|
static void make_grayscale(YV12_BUFFER_CONFIG *yuv) {
|
|
|
|
int r, c;
|
|
|
|
uint8_t *u = yuv->u_buffer;
|
|
|
|
uint8_t *v = yuv->v_buffer;
|
|
|
|
|
2015-01-04 05:46:13 +01:00
|
|
|
for (r = 0; r < yuv->uv_height; ++r) {
|
|
|
|
for (c = 0; c < yuv->uv_width; ++c) {
|
2014-06-18 17:30:07 +02:00
|
|
|
u[c] = UINT8_MAX / 2;
|
|
|
|
v[c] = UINT8_MAX / 2;
|
|
|
|
}
|
2015-01-04 05:46:13 +01:00
|
|
|
u += yuv->uv_stride;
|
|
|
|
v += yuv->uv_stride;
|
2014-06-18 17:30:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|