/* * Copyright (c) 2020, Alliance for Open Media. All rights reserved. * * This source code is subject to the terms of the BSD 2 Clause License and * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License * was not distributed with this source code in the LICENSE file, you can * obtain it at www.aomedia.org/license/software. If the Alliance for Open * Media Patent License 1.0 was not distributed with this source code in the * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
*/
// TODO(kyslov): 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 av1_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) { int r, c; const uint8_t *sig_start = sig; const uint8_t *mc_avg_start = mc_avg;
uint8_t *avg_start = avg; int diff, adj, absdiff, delta; int adj_val[] = { 3, 4, 6 }; int total_adj = 0; int shift_inc = 1;
// 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. 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;
}
// First attempt to apply a strong temporal denoising filter. for (r = 0; r < block_size_high[bs]; ++r) { for (c = 0; c < block_size_wide[bs]; ++c) {
diff = mc_avg[c] - sig[c];
absdiff = abs(diff);
if (absdiff <= absdiff_thresh(bs, increase_denoising)) {
avg[c] = mc_avg[c];
total_adj += diff;
} else { switch (absdiff) { case 4: case 5: case 6: case 7: adj = adj_val[0]; break; case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: adj = adj_val[1]; break; default: adj = adj_val[2];
} if (diff > 0) {
avg[c] = AOMMIN(UINT8_MAX, sig[c] + adj);
total_adj += adj;
} else {
avg[c] = AOMMAX(0, sig[c] - adj);
total_adj -= adj;
}
}
}
sig += sig_stride;
avg += avg_stride;
mc_avg += mc_avg_stride;
}
// 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)) >>
num_pels_log2_lookup[bs]) +
1;
if (delta >= delta_thresh(bs, increase_denoising)) { return COPY_BLOCK;
}
mc_avg = mc_avg_start;
avg = avg_start;
sig = sig_start; for (r = 0; r < block_size_high[bs]; ++r) { for (c = 0; c < block_size_wide[bs]; ++c) {
diff = mc_avg[c] - sig[c];
adj = abs(diff); if (adj > delta) {
adj = delta;
} if (diff > 0) { // Diff positive means we made positive adjustment above // (in first try/attempt), so now make negative adjustment to bring // denoised signal down.
avg[c] = AOMMAX(0, avg[c] - adj);
total_adj -= adj;
} else { // Diff negative means we made negative adjustment above // (in first try/attempt), so now make positive adjustment to bring // denoised signal up.
avg[c] = AOMMIN(UINT8_MAX, avg[c] + adj);
total_adj += adj;
}
}
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;
}
static uint8_t *block_start(uint8_t *framebuf, int stride, int mi_row, int mi_col) { return framebuf + (stride * mi_row << 2) + (mi_col << 2);
}
static AV1_DENOISER_DECISION perform_motion_compensation(
AV1_COMMON *const cm, AV1_DENOISER *denoiser, MACROBLOCK *mb, BLOCK_SIZE bs, int increase_denoising, int mi_row, int mi_col, PICK_MODE_CONTEXT *ctx, int motion_magnitude, int *zeromv_filter, int num_spatial_layers, int width, int lst_fb_idx, int gld_fb_idx, int use_svc, int spatial_layer, int use_gf_temporal_ref) { constint sse_diff = (ctx->newmv_sse == UINT_MAX)
? 0
: ((int)ctx->zeromv_sse - (int)ctx->newmv_sse); int frame; int denoise_layer_idx = 0;
MACROBLOCKD *filter_mbd = &mb->e_mbd;
MB_MODE_INFO *mi = filter_mbd->mi[0];
MB_MODE_INFO saved_mi; int i; struct buf_2d saved_dst[MAX_MB_PLANE]; struct buf_2d saved_pre[MAX_MB_PLANE]; // const RefBuffer *saved_block_refs[2];
MV_REFERENCE_FRAME saved_frame;
// If the best reference frame uses inter-prediction and there is enough of a // difference in sum-squared-error, use it. if (frame != INTRA_FRAME && frame != ALTREF_FRAME && frame != GOLDEN_FRAME &&
sse_diff > sse_diff_thresh(bs, increase_denoising, motion_magnitude)) {
mi->ref_frame[0] = ctx->best_reference_frame;
mi->mode = ctx->best_sse_inter_mode;
mi->mv[0] = ctx->best_sse_mv;
} else { // Otherwise, use the zero reference frame.
frame = ctx->best_zeromv_reference_frame;
ctx->newmv_sse = ctx->zeromv_sse; // Bias to last reference. if ((num_spatial_layers > 1 && !use_gf_temporal_ref) ||
frame == ALTREF_FRAME ||
(frame == GOLDEN_FRAME && use_gf_temporal_ref) ||
(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;
}
mi->ref_frame[0] = frame;
mi->mode = GLOBALMV;
mi->mv[0].as_int = 0;
ctx->best_sse_inter_mode = GLOBALMV;
ctx->best_sse_mv.as_int = 0;
*zeromv_filter = 1; if (denoiser->denoising_level > kDenMedium) {
motion_magnitude = 0;
}
}
saved_frame = frame; // When using SVC, we need to map REF_FRAME to the frame buffer index. if (use_svc) { if (frame == LAST_FRAME)
frame = lst_fb_idx + 1; elseif (frame == GOLDEN_FRAME)
frame = gld_fb_idx + 1; // Shift for the second spatial layer. if (num_spatial_layers - spatial_layer == 2)
frame = frame + denoiser->num_ref_frames;
denoise_layer_idx = num_spatial_layers - spatial_layer - 1;
}
// Force copy (no denoise, copy source in denoised buffer) if // running_avg_y[frame] is NULL. if (denoiser->running_avg_y[frame].buffer_alloc == NULL) { // Restore everything to its original state
*mi = saved_mi; return COPY_BLOCK;
}
if (ctx->newmv_sse > sse_thresh(bs, increase_denoising)) { // Restore everything to its original state
*mi = saved_mi; return COPY_BLOCK;
} if (motion_magnitude > (noise_motion_thresh(bs, increase_denoising) << 3)) { // Restore everything to its original state
*mi = saved_mi; return COPY_BLOCK;
}
// We will restore these after motion compensation. for (i = 0; i < MAX_MB_PLANE; ++i) {
saved_pre[i] = filter_mbd->plane[i].pre[0];
saved_dst[i] = filter_mbd->plane[i].dst;
}
// Set the pointers in the MACROBLOCKD to point to the buffers in the denoiser // struct.
set_ref_ptrs(cm, filter_mbd, saved_frame, NONE);
av1_setup_pre_planes(filter_mbd, 0, &(denoiser->running_avg_y[frame]), mi_row,
mi_col, filter_mbd->block_ref_scale_factors[0], 1);
av1_setup_dst_planes(filter_mbd->plane, bs,
&(denoiser->mc_running_avg_y[denoise_layer_idx]), mi_row,
mi_col, 0, 1);
// Restore everything to its original state
*mi = saved_mi; for (i = 0; i < MAX_MB_PLANE; ++i) {
filter_mbd->plane[i].pre[0] = saved_pre[i];
filter_mbd->plane[i].dst = saved_dst[i];
}
return FILTER_BLOCK;
}
void av1_denoiser_denoise(AV1_COMP *cpi, MACROBLOCK *mb, int mi_row, int mi_col,
BLOCK_SIZE bs, PICK_MODE_CONTEXT *ctx,
AV1_DENOISER_DECISION *denoiser_decision, int use_gf_temporal_ref) { int mv_col, mv_row; int motion_magnitude = 0; int zeromv_filter = 0;
AV1_DENOISER *denoiser = &cpi->denoiser;
AV1_DENOISER_DECISION decision = COPY_BLOCK;
if (denoiser->denoising_level == kDenHigh) increase_denoising = 1;
// Copy block if LAST_FRAME is not a reference. // Last doesn't always exist when SVC layers are dynamically changed, e.g. top // spatial layer doesn't have last reference when it's brought up for the // first time on the fly. if (last_is_reference && denoiser->denoising_level >= kDenLow &&
!ctx->sb_skip_denoising)
decision = perform_motion_compensation(
&cpi->common, denoiser, mb, bs, increase_denoising, mi_row, mi_col, ctx,
motion_magnitude, &zeromv_filter, cpi->svc.number_spatial_layers,
cpi->source->y_width, cpi->ppi->rtc_ref.ref_idx[0],
cpi->ppi->rtc_ref.ref_idx[3], cpi->ppi->use_svc,
cpi->svc.spatial_layer_id, use_gf_temporal_ref);
void av1_denoiser_update_frame_info(
AV1_DENOISER *denoiser, YV12_BUFFER_CONFIG src, struct RTC_REF *rtc_ref, struct SVC *svc, FRAME_TYPE frame_type, int refresh_alt_ref_frame, int refresh_golden_frame, int refresh_last_frame, int alt_fb_idx, int gld_fb_idx, int lst_fb_idx, int resized, int svc_refresh_denoiser_buffers, int second_spatial_layer) { constint shift = second_spatial_layer ? denoiser->num_ref_frames : 0; // Copy source into denoised reference buffers on KEY_FRAME or // if the just encoded frame was resized. For SVC, copy source if the base // spatial layer was key frame. if (frame_type == KEY_FRAME || resized != 0 || denoiser->reset ||
svc_refresh_denoiser_buffers) { int i; // Start at 1 so as not to overwrite the INTRA_FRAME for (i = 1; i < denoiser->num_ref_frames; ++i) { if (denoiser->running_avg_y[i + shift].buffer_alloc != NULL)
copy_frame(&denoiser->running_avg_y[i + shift], &src);
}
denoiser->reset = 0; return;
}
if (rtc_ref->set_ref_frame_config) { int i; for (i = 0; i < REF_FRAMES; i++) { if (rtc_ref->refresh[svc->spatial_layer_id] & (1 << i))
copy_frame(&denoiser->running_avg_y[i + 1 + shift],
&denoiser->running_avg_y[INTRA_FRAME + shift]);
}
} else { // 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) {
copy_frame(&denoiser->running_avg_y[alt_fb_idx + 1 + shift],
&denoiser->running_avg_y[INTRA_FRAME + shift]);
} if (refresh_golden_frame) {
copy_frame(&denoiser->running_avg_y[gld_fb_idx + 1 + shift],
&denoiser->running_avg_y[INTRA_FRAME + shift]);
} if (refresh_last_frame) {
copy_frame(&denoiser->running_avg_y[lst_fb_idx + 1 + shift],
&denoiser->running_avg_y[INTRA_FRAME + shift]);
}
} else { if (refresh_alt_ref_frame) {
swap_frame_buffer(&denoiser->running_avg_y[alt_fb_idx + 1 + shift],
&denoiser->running_avg_y[INTRA_FRAME + shift]);
} if (refresh_golden_frame) {
swap_frame_buffer(&denoiser->running_avg_y[gld_fb_idx + 1 + shift],
&denoiser->running_avg_y[INTRA_FRAME + shift]);
} if (refresh_last_frame) {
swap_frame_buffer(&denoiser->running_avg_y[lst_fb_idx + 1 + shift],
&denoiser->running_avg_y[INTRA_FRAME + shift]);
}
}
}
}
int av1_denoiser_realloc_svc(AV1_COMMON *cm, AV1_DENOISER *denoiser, struct RTC_REF *rtc_ref, struct SVC *svc, int svc_buf_shift, int refresh_alt, int refresh_gld, int refresh_lst, int alt_fb_idx, int gld_fb_idx, int lst_fb_idx) { int fail = 0; if (rtc_ref->set_ref_frame_config) { int i; for (i = 0; i < REF_FRAMES; i++) { if (cm->current_frame.frame_type == KEY_FRAME ||
rtc_ref->refresh[svc->spatial_layer_id] & (1 << i)) {
fail = av1_denoiser_realloc_svc_helper(cm, denoiser,
i + 1 + svc_buf_shift);
}
}
} else { if (refresh_alt) { // Increase the frame buffer index by 1 to map it to the buffer index in // the denoiser.
fail = av1_denoiser_realloc_svc_helper(cm, denoiser,
alt_fb_idx + 1 + svc_buf_shift); if (fail) return 1;
} if (refresh_gld) {
fail = av1_denoiser_realloc_svc_helper(cm, denoiser,
gld_fb_idx + 1 + svc_buf_shift); if (fail) return 1;
} if (refresh_lst) {
fail = av1_denoiser_realloc_svc_helper(cm, denoiser,
lst_fb_idx + 1 + svc_buf_shift); if (fail) return 1;
}
} return 0;
}
int av1_denoiser_alloc(AV1_COMMON *cm, struct SVC *svc, AV1_DENOISER *denoiser, int use_svc, int noise_sen, int width, int height, int ssx, int ssy, int use_highbitdepth, int border) { int i, layer, fail, init_num_ref_frames; constint legacy_byte_alignment = 0; int num_layers = 1; int scaled_width = width; int scaled_height = height; if (use_svc) {
LAYER_CONTEXT *lc = &svc->layer_context[svc->spatial_layer_id *
svc->number_temporal_layers +
svc->temporal_layer_id];
av1_get_layer_resolution(width, height, lc->scaling_factor_num,
lc->scaling_factor_den, &scaled_width,
&scaled_height); // For SVC: only denoise at most 2 spatial (highest) layers. if (noise_sen >= 2) // Denoise from one spatial layer below the top.
svc->first_layer_denoise = AOMMAX(svc->number_spatial_layers - 2, 0); else // Only denoise the top spatial layer.
svc->first_layer_denoise = AOMMAX(svc->number_spatial_layers - 1, 0);
num_layers = svc->number_spatial_layers - svc->first_layer_denoise;
}
assert(denoiser != NULL);
denoiser->num_ref_frames = use_svc ? SVC_REF_FRAMES : NONSVC_REF_FRAMES;
init_num_ref_frames = use_svc ? REF_FRAMES : NONSVC_REF_FRAMES;
denoiser->num_layers = num_layers;
CHECK_MEM_ERROR(cm, denoiser->running_avg_y,
aom_calloc(denoiser->num_ref_frames * num_layers, sizeof(denoiser->running_avg_y[0])));
CHECK_MEM_ERROR(
cm, denoiser->mc_running_avg_y,
aom_calloc(num_layers, sizeof(denoiser->mc_running_avg_y[0])));
// TODO(kyslov) Enable when SVC temporal denosing is implemented #if 0 staticvoid force_refresh_longterm_ref(AV1_COMP *const cpi) {
SVC *const svc = &cpi->svc; // If long term reference is used, force refresh of that slot, so // denoiser buffer for long term reference stays in sync. if (svc->use_gf_temporal_ref_current_layer) { int index = svc->spatial_layer_id; if (svc->number_spatial_layers == 3) index = svc->spatial_layer_id - 1;
assert(index >= 0);
cpi->alt_fb_idx = svc->buffer_gf_temporal_ref[index].idx;
cpi->refresh_alt_ref_frame = 1;
}
} #endif
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.