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>
|
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"
|
|
|
|
#include "vp9/encoder/vp9_denoiser.h"
|
2014-06-05 23:00:08 +02:00
|
|
|
|
|
|
|
static const int widths[] = {4, 4, 8, 8, 8, 16, 16, 16, 32, 32, 32, 64, 64};
|
|
|
|
static const int heights[] = {4, 8, 4, 8, 16, 8, 16, 32, 16, 32, 64, 32, 64};
|
|
|
|
|
|
|
|
int vp9_denoiser_filter() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-13 22:58:57 +02:00
|
|
|
static int update_running_avg(const uint8_t *mc_avg, int mc_avg_stride,
|
|
|
|
uint8_t *avg, int avg_stride,
|
|
|
|
const uint8_t *sig, int sig_stride,
|
|
|
|
int increase_denoising, BLOCK_SIZE bs) {
|
2014-06-12 01:22:38 +02:00
|
|
|
int r, c;
|
2014-06-13 01:05:01 +02:00
|
|
|
int diff, adj, absdiff;
|
|
|
|
int shift_inc1 = 0, shift_inc2 = 1;
|
|
|
|
int adj_val[] = {3, 4, 6};
|
2014-06-12 01:22:38 +02:00
|
|
|
int total_adj = 0;
|
|
|
|
|
2014-06-13 01:05:01 +02:00
|
|
|
if (increase_denoising) {
|
|
|
|
shift_inc1 = 1;
|
|
|
|
shift_inc2 = 2;
|
|
|
|
}
|
|
|
|
|
2014-06-12 01:22:38 +02:00
|
|
|
for (r = 0; r < heights[bs]; ++r) {
|
|
|
|
for (c = 0; c < widths[bs]; ++c) {
|
|
|
|
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-13 01:05:01 +02:00
|
|
|
if (absdiff <= 3 + shift_inc1) {
|
|
|
|
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) {
|
|
|
|
avg[c] = MIN(UINT8_MAX, sig[c] + adj);
|
|
|
|
total_adj += adj;
|
|
|
|
} else {
|
|
|
|
avg[c] = MAX(0, sig[c] - adj);
|
|
|
|
total_adj -= adj;
|
|
|
|
}
|
2014-06-12 01:22:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
sig += sig_stride;
|
|
|
|
avg += avg_stride;
|
|
|
|
mc_avg += mc_avg_stride;
|
|
|
|
}
|
|
|
|
return total_adj;
|
|
|
|
}
|
|
|
|
|
2014-06-13 22:58:57 +02:00
|
|
|
static uint8_t *block_start(uint8_t *framebuf, int stride,
|
|
|
|
int mi_row, int mi_col) {
|
2014-06-13 21:43:52 +02:00
|
|
|
return framebuf + (stride * mi_row * 8) + (mi_col * 8);
|
2014-06-12 23:45:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void copy_block(uint8_t *dest, int dest_stride,
|
|
|
|
uint8_t *src, int src_stride, BLOCK_SIZE bs) {
|
|
|
|
int r, c;
|
|
|
|
for (r = 0; r < heights[bs]; ++r) {
|
|
|
|
for (c = 0; c < widths[bs]; ++c) {
|
|
|
|
dest[c] = src[c];
|
|
|
|
}
|
|
|
|
dest += dest_stride;
|
|
|
|
src += src_stride;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-13 21:43:52 +02:00
|
|
|
void vp9_denoiser_denoise(VP9_DENOISER *denoiser, MACROBLOCK *mb,
|
2014-06-05 23:00:08 +02:00
|
|
|
int mi_row, int mi_col, BLOCK_SIZE bs) {
|
2014-06-12 23:45:42 +02:00
|
|
|
int decision = COPY_BLOCK;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2014-06-14 01:02:36 +02:00
|
|
|
|
|
|
|
update_running_avg(mc_avg_start, mc_avg.y_stride, avg_start, avg.y_stride,
|
2014-06-13 01:05:01 +02:00
|
|
|
mb->plane[0].src.buf, mb->plane[0].src.stride, 0, bs);
|
2014-06-12 23:45:42 +02:00
|
|
|
|
|
|
|
if (decision == FILTER_BLOCK) {
|
2014-06-13 22:58:57 +02:00
|
|
|
// TODO(tkopp)
|
2014-06-12 23:45:42 +02:00
|
|
|
}
|
|
|
|
if (decision == COPY_BLOCK) {
|
2014-06-14 01:02:36 +02:00
|
|
|
copy_block(avg_start, avg.y_stride, src.buf, src.stride, bs);
|
2014-06-12 23:45:42 +02:00
|
|
|
}
|
2014-06-05 23:00:08 +02:00
|
|
|
}
|
|
|
|
|
2014-06-13 22:58:57 +02:00
|
|
|
static void copy_frame(YV12_BUFFER_CONFIG dest, const YV12_BUFFER_CONFIG src) {
|
2014-06-13 21:43:52 +02:00
|
|
|
int r, c;
|
2014-06-13 22:58:57 +02:00
|
|
|
const uint8_t *srcbuf = src.y_buffer;
|
2014-06-13 21:43:52 +02:00
|
|
|
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) {
|
|
|
|
for (c = 0; c < dest.y_width; ++c) {
|
|
|
|
destbuf[c] = srcbuf[c];
|
|
|
|
}
|
|
|
|
destbuf += dest.y_stride;
|
|
|
|
srcbuf += src.y_stride;
|
|
|
|
}
|
2014-06-12 22:53:49 +02: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,
|
|
|
|
int refresh_last_frame) {
|
2014-06-12 22:53:49 +02:00
|
|
|
if (frame_type == KEY_FRAME) {
|
2014-06-13 22:58:57 +02:00
|
|
|
int i;
|
2014-06-12 22:53:49 +02:00
|
|
|
copy_frame(denoiser->running_avg_y[LAST_FRAME], src);
|
|
|
|
for (i = 2; i < MAX_REF_FRAMES - 1; i++) {
|
|
|
|
copy_frame(denoiser->running_avg_y[i],
|
|
|
|
denoiser->running_avg_y[LAST_FRAME]);
|
|
|
|
}
|
2014-06-13 22:58:57 +02:00
|
|
|
} else { /* For non key frames */
|
2014-06-12 22:53:49 +02:00
|
|
|
if (refresh_alt_ref_frame) {
|
|
|
|
copy_frame(denoiser->running_avg_y[ALTREF_FRAME],
|
|
|
|
denoiser->running_avg_y[INTRA_FRAME]);
|
|
|
|
}
|
|
|
|
if (refresh_golden_frame) {
|
|
|
|
copy_frame(denoiser->running_avg_y[GOLDEN_FRAME],
|
|
|
|
denoiser->running_avg_y[INTRA_FRAME]);
|
|
|
|
}
|
|
|
|
if (refresh_last_frame) {
|
|
|
|
copy_frame(denoiser->running_avg_y[LAST_FRAME],
|
|
|
|
denoiser->running_avg_y[INTRA_FRAME]);
|
|
|
|
}
|
|
|
|
}
|
2014-06-05 23:00:08 +02:00
|
|
|
}
|
|
|
|
|
2014-06-17 21:51:39 +02:00
|
|
|
void vp9_denoiser_reset_frame_stats(VP9_DENOISER *denoiser) {
|
|
|
|
denoiser->zero_mv_sse = UINT_MAX;
|
|
|
|
denoiser->best_sse = UINT_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
void vp9_denoiser_update_frame_stats(VP9_DENOISER *denoiser, MB_MODE_INFO *mbmi,
|
|
|
|
unsigned int sse, PREDICTION_MODE mode) {
|
|
|
|
// TODO(tkopp): Use both MVs if possible
|
|
|
|
if (mbmi->mv[0].as_int == 0 && sse < denoiser->zero_mv_sse) {
|
|
|
|
denoiser->zero_mv_sse = sse;
|
|
|
|
denoiser->best_zeromv_reference_frame = mbmi->ref_frame[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mbmi->mv[0].as_int != 0 && sse < denoiser->best_sse) {
|
|
|
|
denoiser->best_sse = sse;
|
|
|
|
denoiser->best_sse_inter_mode = mode;
|
|
|
|
denoiser->best_sse_mv = mbmi->mv[0];
|
|
|
|
denoiser->best_reference_frame = mbmi->ref_frame[0];
|
|
|
|
}
|
2014-06-05 23:00:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int vp9_denoiser_alloc(VP9_DENOISER *denoiser, int width, int height,
|
2014-06-12 22:53:49 +02:00
|
|
|
int ssx, int ssy, int border) {
|
|
|
|
int i, fail;
|
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) {
|
|
|
|
fail = vp9_alloc_frame_buffer(&denoiser->running_avg_y[i], width, height,
|
2014-06-13 22:58:57 +02:00
|
|
|
ssx, ssy, border);
|
2014-06-12 22:53:49 +02:00
|
|
|
if (fail) {
|
|
|
|
vp9_denoiser_free(denoiser);
|
|
|
|
return 1;
|
|
|
|
}
|
2014-06-12 01:12:21 +02:00
|
|
|
}
|
|
|
|
|
2014-06-12 22:53:49 +02:00
|
|
|
fail = vp9_alloc_frame_buffer(&denoiser->mc_running_avg_y, width, height,
|
2014-06-13 22:58:57 +02:00
|
|
|
ssx, ssy, border);
|
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-17 21:51:39 +02:00
|
|
|
denoiser->increase_denoising = 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;
|
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) {
|
|
|
|
if (&denoiser->running_avg_y[i] != NULL) {
|
|
|
|
vp9_free_frame_buffer(&denoiser->running_avg_y[i]);
|
|
|
|
}
|
2014-06-12 01:12:21 +02:00
|
|
|
}
|
2014-06-12 22:53:49 +02:00
|
|
|
if (&denoiser->mc_running_avg_y != NULL) {
|
|
|
|
vp9_free_frame_buffer(&denoiser->mc_running_avg_y);
|
2014-06-12 01:12:21 +02:00
|
|
|
}
|
2014-06-05 23:00:08 +02:00
|
|
|
}
|